我正在尝试编译这段代码:
#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::stringstream
有 move 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/