この単純なクラスを移動可能にするにはどうすればよいですか? 私が正しいと思っていたことが、間違いの壁を生むだけです...
#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
...そして、文字列ストリームのさまざまな基本クラスに対して呼び出されているコピー割り当てに関するエラーの壁を取得します。
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 base classes: Missing move and swap operations on basic_ios." 呪い!