http://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives


Basically copying and pasting from Bjarne Stroustrup's "The C++ Programming Language 4th Edition":

List initialization does not allow narrowing (§iso.8.5.4). That is:

  • An integer cannot be converted to another integer that cannot hold its value. For example, char to int is allowed, but not int to char.
  • A floating-point value cannot be converted to another floating-point type that cannot hold its value. For example, float to double is allowed, but not double to float.
  • A floating-point value cannot be converted to an integer type.
  • An integer value cannot be converted to a floating-point type.

Example:

void fun(double val, int val2) { int x2 = val; // if val==7.9, x2 becomes 7 (bad) char c2 = val2; // if val2==1025, c2 becomes 1 (bad) int x3 {val}; // error: possible truncation (good) char c3 {val2}; // error: possible narrowing (good) char c4 {24}; // OK: 24 can be represented exactly as a char (good) char c5 {264}; // error (assuming 8-bit chars): 264 cannot be // represented as a char (good) int x4 {2.0}; // error: no double to int value conversion (good) }

The only situation where = is preferred over {} is when using auto keyword to get the type determined by the initializer.

Example:

auto z1 {99}; // z1 is an initializer_list<int> auto z2 = 99; // z2 is an int


이렇다고 한다~


type check을 좀더 엄격하게 한다고 보면 될듯.


아 참고로


type을 Widget이라고 가정하고

Widget func(args); 라고하면

이게 args라는 인자를 받아서 Widget type으로 리턴하는 함수를 호출하는 거를 선언해 놓은것인지

Widget type에 args를 인자로 주어서 새로운 인스턴스를 생성하는 것인지 헷갈릴 수 있는데

Widget func{args}; 라고 쓰면 정확하게 인스턴스 생성이라는 것을 알 수 있음 ㅇㅇ.