different ways to call constructors

Greetings,
may i ask whats the difference between calling

 
  Class instance(stuff);

and
 
  Class instance{stuff};

?
There is no difference except that implicit narrowing conversions are not allowed within the braces.

1
2
3
int i(5.3) ; // fine; 5.3 narrowed to int

int j{5.3} ; // *** error: implicit narrowing conversion double to int inside {} 


BetweenClass instance( stuff1, Stuff2 ); and Class instance{ stuff1, Stuff2 };
there is another difference: initializer-list constructors are also considered when braces are used.

See:
http://www.stroustrup.com/C++11FAQ.html#uniform-init
http://www.stroustrup.com/C++11FAQ.html#init-list
Ok, thanks a lot! (for the references too)
Last edited on
there is another difference: initializer-list constructors are also considered when braces are used.

This can sometimes lead to inconsistencies.

1
2
std::vector<std::string> vec1 {123}; // vector containing 123 empty strings
std::vector<int> vec2 {123}; // vector containing one int that has value 123 

you suddenly killed my enthusiasm and brought me back to the classic way xD
you suddenly killed my enthusiasm and brought me back to the classic way xD


Prefer braces.

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es23-prefer-the--initializer-syntax
Topic archived. No new replies allowed.