Return std :: stringstream - Compilation failed

I am trying to compile this code:

#include <sstream>

std::stringstream foo() {
  std::stringstream log;
  log << "Hello there\n";
  return log;
}

      

GCC 4.9.2

gives me the following error (s -std=c++11

):

[x86-64 gcc 4.9.2] error: use of deleted function
    'std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)'

      

Here's an example.

Since it std::stringstream

does move constructor

, why is the copy constructor called instead of the move constructor?

Note: from GCC 5

code compiles correctly: see here .

+3


source to share


1 answer


If we look at the changes in GCC 5 , we can see:

Full C ++ 11 support including the following new features:

  • std :: deque and std :: vector satisfy the requirements of an allocator-supporting container;
  • movable and pluggable iostream classes;

...



The change to bold is what makes your code compile to GCC 5 and not compile to 4.9, the move constructor just hasn't been implemented for std::stringstream

.

+5


source







All Articles