Copy-initialization of a C ++ class type

If i write

T t = T(); 

T is the class.

I think this calls the default constructor T, and then the copy assignment operator. But the compiler can get rid of the job.

I am trying to find a description of this behavior written in the C ++ standard, but I cannot find it. Could you tell me the right place in the standard?

I ask about this because I am asked to replace this:

 T t; 

from

 T t = T(); 

because of the coding rules checker.

and it happens that the class T is not copyable and has a private copy constructor and copy assignment operator ... Therefore, I would like to see that the compiler really always gets rid of the copy in this case.

edit I was misled by something strange: the noncompyable class actually inherited from boost :: noncopyable in which case it compiles. But if I declare the copy constructor and the copy assignment operator private, it does not compile. Exemple. This compiles:

 class BA { protected: BA() {} ~BA() {} private: BA( const BA& ); const BA& operator=( const BA& ); }; class A : BA { }; int main( void ) { A a = A(); return 0; } 

and the following:

 class A { public: A() {} ~A() {} private: A( const A& ); const A& operator=( const A& ); }; int main( void ) { A a = A(); return 0; } 
+3
c ++
source share
4 answers

Since you requested a standard C ++ quote, here it is:

12.8 copying class objects

fifteen

* When certain criteria are met, implementations are allowed to omit the copy construction of the class object, even if the copy constructor and / or object destructor have side effects. In such cases, the implementation considers the source and purpose of the omitted copy operation as just two different ways of accessing the same object, and the destruction of this object occurs at later times when two objects would be destroyed without optimization .111). This permission for copy operations is permitted in the following cases (which can be combined to eliminate multiple copies):

- in the return statement in a function with the type of the returned class, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the returned type of the function, the copy operation can omit the construction of the automatic object directly into the return value of the function.

- when the object of the temporary class that was not attached to the link (12.2) is copied to the class object with the same cv-unqualified type, the copy operation can be omitted by creating a temporary object directly in the target missing copy *

Example:

 class Thing { public: Thing(); หœThing(); Thing(const Thing&); }; Thing f() { Thing t; return t; } Thing t2 = f(); 

Here, the elite criteria can be combined to eliminate two calls to the Thing copy constructor:
copying the local automatic object t to a temporary object for the return value of the function f () and copying this temporary object to object t2. In fact, the construction of a local object t can be considered as a direct initialization of the global object t2, and that the destruction of objects will occur when the program exits.

+1
source share

This is no-arg, creating a temporary and then a copy, building it on t and not copying it (8.5 / 14).

A copy of ctor can be deleted, but should be available in any case (12.8 / 14-15)

+7
source share

This is called Return Value Optimization . This article also mentions the relevant C ++ paragraphs in the footnotes.

You can always be more explicit in your processing of an object if you want to control the behavior more precisely, for example, allocate a temporary object on the stack through alloca, and then call a new placement operator.

+1
source share

In C ++ 0x you can replace T t; on T t{}; .

+1
source share

All Articles