Return a std::stringstream - 编译失败

Return a std::stringstream - Compilation Fail

我正在尝试编译这段代码:

#include <sstream>

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

GCC 4.9.2 给我以下错误(-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一个例子。

既然std::stringstreammove constructor,为什么调用复制构造函数,而不是移动构造函数?

注意:来自GCC 5的代码编译正确:see here.

如果我们看一下 GCC 5 changes 我们可以看到:

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;

...

粗体字的变化是导致您的代码在 GCC 5 上编译而在 4.9 上编译失败的原因,std::stringstream.

的移动构造函数尚未实现