次の点を考慮してください。
std::string make_what_string( const std::string &id );
struct basic_foo
{
basic_foo( std::string message, std::string id );
};
struct foo
: public basic_foo
{
foo::foo( std::string id)
: basic_foo( make_what_string( id ), std::move( id ) ) // Is this valid?
{
}
};
C++ ではパラメータの評価順序が規定されていないため、
basic_foo( make_what_string( id ), std::move( id ) )
上記のコードは有効です。
私はそれstd::move
がキャストにすぎないことを知っていますが、 std::string move ctor はいつ実行されますか? すべての引数が評価された後、基本コンストラクターを呼び出す時が来ましたか? それとも、これはパラメータの評価中に行われますか? 言い換えると:
コンパイラはこれを行いますか:
std::string &&tmp2 = std::move(id);
std::string tmp1 = make_what_string(id);
basic_foo(tmp1, tmp2);
これは有効です。またはこれ:
std::string tmp2 = std::move(id);
std::string tmp1 = make_what_string(id);
basic_foo(tmp1, tmp2);
これは無効です。どちらの場合も、順序は「予期しない」ものであることに注意してください。