Take the 2-minute tour ×

Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization

T t(e); // direct initialization
T t = e; // copy initialization
share|improve this question
What is an implicit assignment? Do you mean default assignment operator? –  ybungalobill Nov 27 '10 at 20:02
What do you mean by "implicit assignment". –  Cheers and hth. - Alf Nov 27 '10 at 20:03
@Mike: Neither of those is assignment, they're both initialization. –  Stuart Golodetz Nov 27 '10 at 20:35
@Mike: none of these are assignments. You just made up the term. They are copy initializations, and as such they are the same. –  ybungalobill Nov 27 '10 at 20:36
I've fixed your post to be nearer to what you wanted to ask. –  Johannes Schaub - litb Nov 28 '10 at 19:45

2 Answers 2

The actual names of the things you describe is not implicit and explicit assignment but :

  • Copy-initialization : T x = a;
  • Direct-initialization : T x(a);

They are not equivalent, most notably in contexts where a conversion is required, for example when T is of class type and a is of a different type (see Alf comment for examples of contexts which don't even involve conversion). Consider the following code :

class Test
    explicit Test(int i) { /* ... */ }

int main()
    Test t(0);  // OK : calls Test::Test(int)
    Test u = 0; // KO : constructor is marked explicit

To paraphrase the standard (8.5/14) :

  • For direct-initialization and copy-initialization where the source type is the same as, or a derived class of, the class of destination, constructors are considered
  • For other copy-initialization cases, like the second line of main in my example, user-defined conversion sequence are considered. As the use of the Test constructor for implicit conversion was disallowed by the explicit keyword, the second line fails to compile.
share|improve this answer
"They are not equivalent in contexts where a conversion is required" -- there are also other situations where they're not equivalent. –  Cheers and hth. - Alf Nov 28 '10 at 6:29
@Alf: I wasn't sure when I wrote this sentence, but I couldn't find any example. I might be wrong on the terminology here, because what I see in your answer is a conversion (through what the standard calls a converting constructor), and is actually very similar to my answer (except for the curly braces part which I haven't covered). What am I missing ? –  icecrime Nov 28 '10 at 9:09
well, let's say a is the expression T(). :-) Then the copy initialization syntactical form is mostly equivalent to default initialization (except that it may involve a temporary and copying), whereas the direct initialization syntactical form yields interpretation as a function declaration, the "most vexing parse". Also, consider the case of an explicit copy constructor. And depending on how vaguely allowed to think here, consider curly braces initializer. So, at least 3 cases. But direct counter-example is the explicitcopy constructor. Cheers, –  Cheers and hth. - Alf Nov 28 '10 at 9:18
@Alf: I see :) No way I would have thought of any of these ! –  icecrime Nov 28 '10 at 9:22

Direct initialization like

std::istringstream  stream( "blah blah" );

is necessary when the type in question, here std::istringstream from the C++ standard library, does not have an accessible copy constructor.

A copy initialization, like

std::istringstream  stream = "blah blah";   //! NOT VALID

requires an accessible copy constructor, because it's performed as if a temporary object is created on the right hand side of =, and as if that temporary is then used to initialize the variable being declared.

In the other direction, in C++98 the copy initialization syntax is needed in order to use curly braces initializers. For example, direct initialization can't be used to initialize an aggregate. But you can use copy initialization with a curly braces initializer:

#include <string>
using namespace std;

struct Answer
    int     nVotes;
    string  description;

int main()
    Answer const  incorrect   = { 26, "they're the same!" };
    Answer const  correct     = { -1, "nah, they're different, actually" };

So, there are significant differences.

I generally prefer copy initialization syntax because of the clarity. But sometimes, as shown above, direct initialization is, unfortunately, necessary. Some people, e.g. C++ textbook author Francis Glassborow, have instead landed on direct initialization as their preferred initialization syntax (I'm not sure why, it's less clear to my eyes, and introduces the "most vexing parse" problem), and for them it's the necessity of copy initialization in some cases, that is unfortunate.

Cheers & hth.,

share|improve this answer
This answer was written in 2010. With C++11 copy initialization syntax can be used in more cases, because it reduces to C++11 move semantics. Also, the direct initialization syntax has been extended to include curly braces list of values. –  Cheers and hth. - Alf May 15 '14 at 22:09

Your Answer


