Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 511
BiagioF Avatar asked Feb 26 '26 01:02

BiagioF


1 Answers

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.

like image 54
Hatted Rooster Avatar answered Feb 28 '26 13:02

Hatted Rooster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!