Mar 15, 2014

[C++][C++11] Compiler generated implicit default copy constructor/move constructor assign/move assign op deprecated.

clang++ -std=c++11 test.cpp -Wdeprecated

if any of copy/move constructor defined:
default constructor must be defined if needed(as before)

If user defined copy constructor:
Move constructor is =delete

If user defined assign operator:
Move assign operator is =delete

If user defined move constructor:
Copy constructor is =delete
assign operator is =delete

If user defined move assign operator:
Copy constructor is =delete
assign operator is =delete

If user defined destructor:
None of the implicit copy/move constructor, assign/move assign operator is defined.
Generate warning of deprecated

Also beware of RVO(copy elision):
copy elision
Want Speed? Pass by Value.

------------------------

§12.8.32 of the C++11 standard:

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 (即target) of the times when the two objects would have been destroyed without the optimization. 
This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which maybe combined to eliminate multiple copies):
  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object 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 (return type要與function return type宣告的一致, 且無CV qualify) 
  • in a throw-expression, when the operand is the name of a non-volatile automatic object whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object 
  • 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(傳入copy/move constructor若為temporary object, 且無CV qualify,則可omit) 
  • when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy/move operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.
Reference:
Everything You Ever Wanted to Know About Move Semantics


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.