T x(value) is usually the better choice because it will directly initialize x with value, whereas T x = value might create a temporary depending on the type of value.
In the special case where value is of type T though, my guess is that the expression T x = value will always result in exactly one copy constructor call. Am I correct?
I’ve asked this question because I’m starting to think that the first syntax is too ugly and harder to understand, especially when value is the result of a function call.
e.g:
const std::string path(attributes.data(pathAttrib));const std::string path = attributes.data(pathAttrib);
From the standard, copy-initialization for class types where the cv-unqualified type of the source type is the same as, or a derived class of the destination, has exactly the same behaviour as direct-initialization. The description of these two cases introduce a single paragraph describing the required behaviour which is that only constructors for the destination type are considered and the constructor chosen is used to initialize the destination with the initializer expression as argument.
No extra temporary is allowed in these cases.
Neither form of initialization prevent the optimizations described in 12.8 [class.copy] from occuring. Though a non-normative example, the example in 12.8/15 uses the copy-initialization form of initializer to demonstrate the elimination of two copies resulting from a function returning a local variable by value to an object initializer. This means that if
valuein your example is a temporary of typeTthen it – and the copy operation tox– may be eliminated.