【问题标题】:why doesn't c++ uses RVO when returning local std::stringstream?为什么 c++ 在返回本地 std::stringstream 时不使用 RVO?
【发布时间】:2016-07-08 01:37:16
【问题描述】:

我已经阅读了很多关于 rvalue 和在 C++ 中返回局部变量 >= 11 的信息。据我了解,“仅按值返回,不要使用移动/前进,不要将 && 添加到方法签名和编译器会为你优化”。

好的,我希望它发生:

#include <sstream>

std::stringstream GetStream() {
    std::stringstream s("test");
    return s;
}

auto main() -> int {
    auto s = GetStream();
}

我觉得不错

error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
     return s;

错误。我不明白,为什么它会尝试复制构造函数?难道它不使用移动构造函数和 c++11 中的所有好东西吗? 我使用“--std=c++14”。

【问题讨论】:

  • 这个编译without error。你用的是哪个编译器?
  • 我的水晶球告诉我您使用的是 GCC 4.9,它不支持 stringstream 的移动语义。如果你想使用 C++14,那么你应该使用支持它的编译器。
  • 是的,你是在正确的方式。 gcc 4.9.3.
  • 我希望我能像@JonathanWakely 一样了解
  • @erip,有三个线索:我将错误消息识别为 GCC 之一;第一个支持“-std=c++14”选项的 GCC 版本是 4.9.0;我知道我为 GCC 5 添加了移动语义到stringstream;所以它必须是 GCC 4.9.x :)

标签: c++ c++11 move-semantics rvalue rvo


【解决方案1】:

好的,这是我的 gcc (4.9.3) 版本中的一个错误。似乎固定在> = 5中。 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316

【讨论】:

  • 作为54316的记者,很高兴你找到了它;)
猜你喜欢
  • 2018-12-03
  • 2013-10-16
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多