【发布时间】: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