【发布时间】:2013-03-18 19:28:22
【问题描述】:
考虑以下几点:
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
移动 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);
这是无效的。请注意,在这两种情况下,订单都是“意外的” 一个。
【问题讨论】:
-
其实代码是有效的。但是,我相信您的意思是在
base_foo的构造函数中通过右值引用而不是值来获取字符串id(不是吗?)。 -
@CassioNeri,代码不符合预期:)
-
是的,我在午餐时间意识到了这一点。 :-) 非常好的问题。
标签: c++ c++11 move-semantics operator-precedence