61591

Lvalue istringstream Required for istream_iterator?

Question:

Given a string foo in Visual Studio I can break the words into a vector by doing:

vector fooVec{ istream_iterator<string>(istringstream(foo)), istream_iterator<string>() };

But this won't compile in gcc 5.1. I get the error:

<blockquote>

invalid initialization of non-const reference of type std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>&} from an rvalue of type std::basic_istream<char>

</blockquote>

Now I know that gcc had <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316" rel="nofollow">a bug</a> that was fixed by our own <a href="https://stackoverflow.com/users/981959/jonathan-wakely" rel="nofollow">Jonathan Wakely</a>. Is this an extension of that bug or should it be illegal for me to use an Rvalue istringstream here?

Answer1:

This is not a gcc bug but an <a href="https://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug" rel="nofollow">evil MSVC extension</a>. <a href="http://en.cppreference.com/w/cpp/iterator/istream_iterator/istream_iterator" rel="nofollow">std::istream_iterator::istream_iteraor()</a> requires an lvalue reference. Since istringstream(foo) is a temporary gcc correctly tells you you cannot bind the temporary to the lvalue reference.

The reason this works on MSVC is that previously mentioned extension that allows temporaries to be bound to lvalue references. This allows the non standard compliant code to work on MSVC.

So to answer

<blockquote>

Is this an extension of that bug or should it be illegal for me to use an Rvalue istringstream here?

</blockquote>

No this is not a bug and you need a non-temporary stream here to construct the istream_iterator.

Recommend

  • Demonstration of noskipws in C++
  • ld: duplicate symbol
  • Why aren't my destructors called when throwing from a win32 timer callback?
  • Ternary operator evaluation order
  • Meteor.js Handlebars template logic operators
  • Spock @Unroll annotation
  • Regex - Match Last Occurance
  • networkstream “cannot access a disposed object” when using newly created networkstream
  • IE8 stops network access after 5 long polling request
  • Context menu disappears when opened with Key.App
  • Get sibling of Vaadin Tree Item?
  • Deserialize Dictionary
  • Typecasting `this` of a base class template to its derived class
  • Initialization section of the package
  • MRO with multiple inheritance in python
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • CakePHP ACL tutorial initDB function warnings
  • What is the purpose of TaskExecutor in spring?
  • C: Incompatible pointer type initializing
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Using variable in a value field in jMeter
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • ORA-29908: missing primary invocation for ancillary operator
  • Numpy divide by zero. Why?
  • Is there a mandatory requirement to switch app.yaml?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Binding checkboxes to object values in AngularJs