struct A { A() = default; A(const A&) { std::cout << "copied" << std::endl; } A(A&&) { std::cout << "moved" << std::endl; } }; std::pair<A,A> get_pair() { std::pair<A,A> p; return p; } std::tuple<A,A> get_tuple() { std::pair<A,A> get_tuple_moved() { std::pair<A,A> p; return std::move(p); }
有了这个,以下电话:@H_404_5@
get_pair(); get_tuple(); get_tuple_moved();
moved moved copied copied moved moved
See MCVE in action.@H_404_5@
get_pair的结果是移动构建的,正如预期的那样. NRVO也可能完全消除了这一举动,但现在不在于这个问题.@H_404_5@
get_tuple_moved的结果也是移动构造的,它被明确指定为这样.然而,get_tuple的结果是复制构造的,这对我来说是完全不明显的.@H_404_5@
我认为传递给return语句的任何表达式都可以被认为是对它有隐含的动作,因为编译器知道它即将超出范围.好像我错了有人可以澄清,这里发生了什么?@H_404_5@
另请参见相关但不同的问题:When should std::move be used on a function return value?@H_404_5@
解决方法
相关的措辞可以在[class.copy] / p32中找到:@H_404_5@
When the criteria for elision of a copy/move operation are met,[..],or when the expression in a return statement is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body [..],overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.@H_404_5@
所以在C14中,所有的输出都应该来自A的移动构造器.@H_404_5@
clang和gcc的中继版本已经实现了这一变化.要在C 11模式下获得相同的行为,您需要在return语句中使用一个显式的std :: move().@H_404_5@