我已经阅读了很多关于 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”。