38

We can initialize the variable in two ways in C++

One:

int abc = 7;

Two:

int abc {7};

What is the difference between these two methods? Does the compiler treat them differently, or is there a difference in the way the code is executed?

2
  • 4
  • 3
    For int specifically, the only difference is that {} prohibits narrowing conversion, e.g. from a literal that's too large to fit into an int. For class types, it might get complicated depending on how detailed you want to know the differences.
    – dyp
    Jan 15, 2014 at 23:09

3 Answers 3

55

Short version

Initialization via {..} is list-initialization, which prohibits narrowing conversions. For example, if LLONG_MAX is the maximum value of an long long int, and your int cannot represent that:

int x = LLONG_MAX;  // probably accepted with a warning
int x {LLONG_MAX};  // error

Similarly:

long long y = /*something*/;

int x = y;  // accepted, maybe with a warning
int x {y};  // error

Long version

An initialization of the form

T x = a;

is copy-initialization; an initialization of either form

T x(a);
T x{a};

is direct-initialization, [dcl.init]/15-16.

[dcl.init]/14 then says:

The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type; see below.

So for non-class types, the form of the initialization doesn't matter. However, there's a difference between these two direct-initializations:

T x(a);  // 1
T x{a};  // 2

and similarly, between these two copy-initializations:

T x = a;    // 1
T x = {a};  // 2

Namely, the ones with {..} use list-initialization. The {..} is called a braced-init-list.

So, when you compare T x = a; to T x {a};, there are two differences: copy- vs. direct-initialization, and "non-list-" vs. list-initialization. As already mentioned by others and in the quote above, for non-class types T, there's no difference between copy- and direct-init. However, there's a difference between list-init and no list-init. That is, we could as well compare

int x (a);
int x {a};

List-initialization in this case prohibits narrowing conversions. Narrowing conversions are defined in [dcl.init.list]/7 as:

A narrowing conversion is an implicit conversion

  • from a floating-point type to an integer type, or

  • from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented (even if it cannot be represented exactly), or

  • from an integer type or unscoped enumeration type to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type, or

  • from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.

8
  • 2
    What about this kind of initialization using both parens and braces: std::random_device{}() ?
    – moooeeeep
    Jan 24, 2017 at 9:16
  • 6
    @moooeeeep This is not a separate kind of initialization. It constructs an temporary of type std::random_device by using the expression std::random_device{}, and then calls that object's overloaded operator(), just like std::random_device rd; rd() would. The random_device has an operator() which invokes the RNG and returns a (pseudo)random number, see en.cppreference.com/w/cpp/numeric/random/random_device/…
    – dyp
    Jan 25, 2017 at 8:33
  • 1
    Nice, thanks! Now that you explained it, it seems obvious.
    – moooeeeep
    Jan 25, 2017 at 8:36
  • I tried to initialize with int b1{2147483648}; . But I do not get error, instead get only the warning "warning: narrowing conversion of '2147483648ll' from 'long long int' to 'int' inside { } [-Wnarrowing]|. Why it is so?
    – Rajesh
    Oct 9, 2017 at 18:38
  • @Rajesh Which compiler and version? Apparently, this was a warning only up until gcc 5. See also: gcc.gnu.org/bugzilla/show_bug.cgi?id=55783
    – dyp
    Oct 10, 2017 at 6:41
4

While for int the existing replies are complete, I painfully found out, that in some cases, there are other differences between the () and {} initializations.

The keyword is that {} is an initializer list.

One such cases is, the std::string initialization with count copies of a char:

std::string stars(5, '*')

will initialize stars as *****, but

std::string stars{5, '*'}

will be read as std::string stars(char(5), '*') and initialize star as * (preceded by an hidden character).

1

The first is the copy initialization, while the second is list initialization.

But, usually copy initialization is less used. Because, if you're doing it by passing objects of user defined types, it just causes bitcopy & hence may not produce intended results if the user defined class uses pointers.

3
  • Not if they have copy constructors, surely? I'm way beyond confused now.
    – RichieHH
    Sep 14, 2021 at 21:31
  • @RichieHH If user defined type has pointers, then one should prefer to write copy constructor along with constructor & destructor (Rule of 3). But, if there's no copy constructor, then it will cause 'shallow copy' & may cause dangling pointers.
    – yuvi
    Sep 15, 2021 at 10:09
  • Exactly. User defined types using pointers should have copy and initialise constructors. Maybe edit your reply. Thanks for getting back.
    – RichieHH
    Sep 16, 2021 at 5:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.