44

Here are two ways to initialize a variable in C++11:

T a {something};
T a = {something};

I tested these two in all scenarios I could think of and I failed to notice a difference. This answer suggests that there is a subtle difference between the two:

For variables I don't pay much attention between the T t = { init }; or T t { init }; styles, I find the difference to be minor and will at worst only result in a helpful compiler message about misusing an explicit constructor.

So, is there any difference between the two?

Community
  • 1
  • 1
Nikita
  • 1,053
  • 1
  • 10
  • 19
  • Does this answer your question? [Why is list initialization (using curly braces) better than the alternatives?](https://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives) – user202729 Jan 29 '21 at 14:35
  • @user202729 I couldn't find an answer there. The accepted answer talks about narrowing restrictions which happen with and without the equals sign. – Nikita Jan 30 '21 at 21:12
  • ... the question asks for basically the same thing, but because of the different wording, they attracts different kinds of answers. Can't hurt to link it here. – user202729 Jan 31 '21 at 00:37
  • (that message is the auto-generated for duplicate flag, by the way) – user202729 Jan 31 '21 at 00:37

1 Answers1

34

The only significant difference I know is in the treatment of explicit constructors:

struct foo
{
    explicit foo(int);
};

foo f0 {42};    // OK
foo f1 = {42};  // not allowed

This is similar to the "traditional" initialization:

foo f0 (42);  // OK
foo f1 = 42;  // not allowed

See [over.match.list]/1.


Apart from that, there's a defect (see CWG 1270) in C++11 that allows brace-elision only for the form T a = {something}

struct aggr
{
    int arr[5];
};

aggr a0 = {1,2,3,4,5};  // OK
aggr a1 {1,2,3,4,5};    // not allowed
dyp
  • 38,334
  • 13
  • 112
  • 177
  • Doesn't a = {something} also result in an extra copy/move? – gvd Dec 23 '13 at 03:46
  • 8
    @gvd No, that's where "traditional" copy-init and copy-list-init differ: `foo f0 = 42;` converts `42` to a prvalue temporary `foo` and direct-initializes `f0` with that temporary (via a possibly elided copy/move), whereas `foo f1 = {42};` selects a constructor of `f1` to be called with `42` (the copy+move ctor may be deleted in this case). – dyp Dec 23 '13 at 11:00