gpt4 book ai didi

c++ - 在 C++11 中,临时 T 是否应该作为参数调用 T(const T&) 或 T(T&&)?

转载 作者:太空狗 更新时间:2023-10-29 23:51:04 25 4
gpt4 key购买 nike

所以,先写代码:

#include <iostream>
#include <utility>

struct X{
int i;
void transform(){}
X() :i(0){std::cout<<"default\n";}
X(const X& src): i(src.i){std::cout<<"copy\n";}
X(X&& msrc) :i(msrc.i){msrc.i=0;std::cout<<"move\n";}
};

X getTransform(const X& src){
X tx(src);
tx.transform();
return tx;
}

int main(){

X x1;// default
X x2(x1); // copy
X x3{std::move(X{})}; // default then move
X x41(getTransform(x2)); // copy in function ,then what?
X x42(std::move(getTransform(x2))); // copy in funciton, then move
X x51( (X()) );//default, then move? or copy?
// extra() for the most vexing problem
X x52(std::move(X())); //default then move
std::cout<<&x41<<"\t"<<&x51<<std::endl;
}

然后从启用了 C++11 功能的 cygwin + gcc 4.8.2 输出:

default
copy
default
move
copy
copy
move
default
default
move
0x22aa70 0x22aa50

我不太明白的是 x41 和 x51 的行。对于 x41,函数调用返回的临时对象应该调用移动构造函数还是拷贝? x51 的相同问题。
第二个问题是,通过查看输出,x41 和 x51 的构造没有调用任何已定义的构造函数,但对象显然是在驻留在内存中时创建的。怎么会这样?

最佳答案

自然地,一个未命名的对象匹配 &&const& 更好。
否则移动语义将不起作用。

现在,对您的默认/复制/移动构造函数的调用比人们可能天真地期望的要少,因为有一个特殊规则允许省略拷贝,而不考虑可观察到的行为(否则必须通过优化来保留) :

12.8 Copying and moving of objects § 31

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.123 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
Still, if it is returned from a function and directly used to initialize an object of the same type, that move will be omitted.
in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value.
— when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move.
- [... 2 more for exception handling]

所以,通过你的列表:

X x1;// default
// That's right
X x2(x1); // copy
// Dito
X x3{std::move(X{})}; // default then move
// Yes. Sometimes it does not pay to call `std::move`
X x41(getTransform(x2)); // copy in function ,then what?
// Copy in function, copy to output, move-construction to x41.
// RVO applies => no copy to output, and no dtor call for auto variable in function
// Copy ellision applies => no move-construction nor dtor of temporary in main
// So, only one time copy-ctor left
X x42(std::move(getTransform(x2))); // copy in funciton, then move
// `std::`move` is bad again
X x51( (X()) );//default, then move? or copy? // extra() for the most vexing problem
// Copy-elision applies: default+move+dtor of temporary
// will be optimized to just default
X x52(std::move(X())); //default then move
// And again `std::`move` is a pessimization

我认为使用 static_cast 可能会避免绑定(bind)临时对象,这意味着移动可以被省略,但没有这样的运气:1376. static_cast of temporary to rvalue reference谢谢@dyp for unearthing this issue .

关于c++ - 在 C++11 中,临时 T 是否应该作为参数调用 T(const T&) 或 T(T&&)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172205/

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