I have been testing with rvalue references and move semantics and want to make sure I understand when a copy should be elided and when it should follow move semantics.
Given the following
class NRVCA
{
public:
NRVCA(int x):
{}
NRVCA(const NRVCA & Rhs)
{}
NRVCA& operator=(const NRVCA& dref)
{}
};
NVCRA GetATemp()
{
return NVCRA(5);
}
NVCRA GetACopy()
{
NVCRA ret(5);
...
return ret;
}
int main()
{
//This call will be elided allays and invoke the single param constructor
NVCRA A = GetATemp();
//This call will be a traditional copy the complier may elide this
// if so the work will be done inline
NVCRA B = GetACopy();
}
In this case move semantics play no part and the only difference from C++03 in c++11 is that rather than the compiler being allowed to elide they are required to elide.
So
Question 1. Under what cases am I guaranteed a copy constructor will or will not be elided.
Question 2. Is there a way to force the compiler to not elide.
Question 3. Is there any logical reason why I would not want the compiler to do so, assuming that you have logically consistent copy operations.
Question 4. If I define a move constructor, a move will occur in cases where the copy is not elided anyway. Should this effect my design of classes.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…