c++ - 返回 std::stringstream - 编译失败

标签 c++ c++11 gcc move-semantics

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

#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 , 为什么调用复制构造函数而不是 move 构造函数?

注意:来自 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 实现 move 构造函数。

关于c++ - 返回 std::stringstream - 编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44524935/

相关文章:

c++ - Apple 的 SpeakHere 演示,混合了 ObjC 和 C++,以及翻译单元

c++ - 未初始化的自动变量强制值到随机值

linux - C++程序的可执行文件是否也包含系统调用的目标代码

c++ - 添加具有默认类型的模板参数后出现歧义调用

检查 C 库的版本(动态加载)

c++ - 无法编译 : unrecognized relocation

c++ - 与有关多态成员的设计决策作斗争

c++ - 这会做默认的 move 操作吗

c++ - std::array 复制语义

c++ - 处理指向模板中成员的指针,同时创建更简单的 `bind` 以与 asio 一起使用