Reputation: 115
If I write
T t = T();
T is a class.
I think this is calling T's default constructor and then the copy assignment operator. But the compiler is allowed to get rid of the assignment.
I'm trying to find the description of this behavior written in the C++ standard, but I can't find it. Could you point me to the right spot in the standard?
I'm asking this because I'm being asked to replace this :
T t;
with
T t = T();
because of a coding rule checking program.
and it happens that the T class is noncopyable and has a private copy constructor and copy assignment operator... So I'd like to see that the compiler is effectively always getting rid of the copy in this case.
edit: I have been mislead by something weird: the noncompyable class was actually inheriting from boost::noncopyable in this case it does compile. But if I declare the copy constructor and 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 does not :
class A
{
public:
A() {}
~A() {}
private:
A( const A& );
const A& operator=( const A& );
};
int main( void )
{
A a = A();
return 0;
}
Upvotes: 3
Views: 413
Reputation: 206526
Since you asked for the C++ standard citation, here it is:
12.8 copying class objects
15
*When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implemen-tation treats the source and target of the omitted copy 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.111)This elision of copy operations is permitted in the following circumstances (which may be 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 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 to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy*
Example:
class Thing
{
public:
Thing();
˜Thing();
Thing(const Thing&);
};
Thing f()
{
Thing t;
return t;
}
Thing t2 = f();
Here the criteria for elision can be combined to eliminate two calls to the copy constructor of class Thing:
the copying of the local automatic object t into the temporary object for the return value of function f() and the copying of that temporary object into object t2. Effectively, the construction of the local object t can be viewed as directly initializing the global object t2, and that object’s destruction will occur at program exit.
Upvotes: 1
Reputation: 77752
This is called Return Value Optimization. This article also mentions the relevant C++ standard paragraphs in the footnotes.
You could always be more explicit in your handling of the object if you want to control the behavior more precisely, like allocating the temporary object on the stack yourself via alloca and then calling the placement new operator.
Upvotes: 1
Reputation: 279255
It's no-arg constructing a temporary and then copy constructing it to t
, not copy assigning it (8.5/14).
The copy ctor can be elided, but must be accessible anyway (12.8/14-15)
Upvotes: 7