BiagioF
BiagioF

Reputation: 9725

Return a std::stringstream - Compilation Fail

I'm 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 (with -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 an example.

Since the std::stringstream has move constructor, why the copy constructor is invoked, instead of the move constructor?

Note: from GCC 5 the code compile correctly: see here.

Upvotes: 3

Views: 152

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36503

If we take a look at the GCC 5 changes we can see:

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

  • std::deque and std::vector meet the allocator-aware container requirements;
  • movable and swappable iostream classes;

...

The change in bold is what's making your code compile on GCC 5 and fail to compile on 4.9, the move constructor simply wasn't implemented yet for std::stringstream.

Upvotes: 5

Related Questions