【问题标题】:Return a std::stringstream - Compilation Fail返回一个 std::stringstream - 编译失败
【发布时间】:2017-11-15 10:26:16
【问题描述】:

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

#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

【问题讨论】:

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


    【解决方案1】:

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

    完全支持 C++11,包括以下新功能:

    • std::deque 和 std::vector 满足分配器感知容器要求;
    • 可移动和可交换的 iostream 类;

    ...

    粗体的变化是什么让你的代码在 GCC 5 上编译而在 4.9 上编译失败,移动构造函数还没有为 std::stringstream 实现。

    【讨论】:

    • 我认为既然选项-std=c++11, GCC 4.9.2 完全支持整个标准。
    猜你喜欢
    • 1970-01-01
    • 2012-12-09
    • 2019-11-17
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多