TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OLC#007

This rule is Obsolete
Synopsis:Use direct- instead of copy-initialization in object declarations of class type
Language:C++
Severity Level:7
Category:Object Life Cycle


Description:

An object obj of type T can be initialized with a value val as follows:

T obj = val;    // copy-initialization, implicit
T obj = T(val); // copy-initialization, explicit
T obj(T(val));  // copy-initialization, explicit
T obj(val);     // direct-initialization

The direct-initialization form works for an arbitrary amount of constructor arguments.

The copy-initialization should not be used because it may require an additional construction of a temporary object that is used to invoke the copy-constructor of obj. The direct-initialization does not require such a copy-construction. Furthermore, the first form of copy-initialization is not possible if T's single argument constructors are declared as explicit as required by [INT#001]. Finally, T might not even have a copy-constructor, making direct-initialization the only choice.



Literature References:
Ellemtel Rule 41