gpt4 book ai didi

c++ - 为什么包含 std::stringstream 的类的 move 语义会导致编译器错误?

转载 作者:IT老高 更新时间:2023-10-28 13:00:24 26 4
gpt4 key购买 nike

如何使这个简单的类可 move ?我认为是正确的只会产生一堵错误的墙......

#include <iostream>
#include <sstream>
#include <utility>

class message
{
public:
message() = default;

// Move constructor
message( message &&other ) :
stream_( std::move( other.stream_ ) ) // Nope
{}

// Move assignment
message &operator=( message &&other )
{
if ( this != &other )
{
stream_ = std::move( other.stream_ ); // Nope #2
}
return *this;
}

private:
message( const message & ) = delete;
message &operator=( const message & ) = delete;

std::stringstream stream_;
// Other member variables omitted
};

int main()
{
message m;

return 0;
}

编译:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
$ g++ -Wall -Wextra -std=c++0x move.cpp -o move

... 并得到一堵关于为 stringstream 的各种基类调用的复制分配的错误。

move.cpp: In constructor ‘message::message(message&&)’:
move.cpp:12:40: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
In file included from move.cpp:2:0:
/usr/include/c++/4.6/sstream:483:11: error: ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/sstream:483:11: error: use of deleted function ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’
In file included from /usr/include/c++/4.6/iostream:41:0,
from move.cpp:1:
/usr/include/c++/4.6/istream:774:11: error: ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.6/istream:57:11: error: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/istream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
[SNIP]

...这持续了好几页。

我的代码有什么问题?

更新 1:Clang 3.0 失败,结果相似。
更新 2:g++ 4.7 也失败了。
更新 3:使用答案作为指导,我发现:c++11 status in libstdc++ - “27.5 Iostreams 基类:basic_ios 上缺少 move 和交换操作。”诅咒!

最佳答案

更新

需要按照 C++11/14 标准工作。 GCC 5.0 做对了,下面提到的错误已解决。感谢 GCC 团队!

原答案

到目前为止,它是 gcc(或 Xeo 指出 libstdc++)中缺少的功能。

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316

按照标准,

typedef basic_stringstream<char> stringstream;

从 27.8.5 开始,有一个公共(public) move 构造函数

basic_stringstream(basic_stringstream&& rhs);

我可以通过 gcc 4.7 -std=c++11 确认问题。 , Ubuntu 12.04.

std::stringstream a;
std::stringstream b=std::move(a);

读取包含文件include/std/sstream我发现没有 move 构造函数或任何提及 C++0x 或 C++11。 (与确实有效的 std::string 进行比较。)

添加(模拟) move 构造函数:

basic_stringstream(basic_stringstream&& rhs){}

将错误减少到只有树线,但是

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

仍然存在。

解决方法

使用 std::unique_ptr<std::stringstream>反而。最好是用 make_unique 初始化它c++14 自带的,或者以我的博客 cpp11style-no-new-delete 为例(这是一个较早的版本,但它可以很好地用于此目的)。

关于c++ - 为什么包含 std::stringstream 的类的 move 语义会导致编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12015899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com