+ 2

What is the diffrence between "int k=2; " and "int k{2};"

why do we need to declare the value in " {}" ??

23rd May 2017, 6:10 AM
U L Knw Me soon😉😋
U L Knw Me soon😉😋 - avatar
1 ответ
+ 2
Initializing with braces is called List initialization, which was introduced in C++ in 2011. Which means it doesn't allow narrowing, I'm gonna copy/paste from the Author's book itself: • 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. The only situation where = is preferred over {} is when using auto keyword to get the type determined by the initializer. It takes time to kick in. Research more on Stackoverflow, people have amazing and more thorough answers.
23rd May 2017, 8:48 AM
Amiko Megrelishvili
Amiko Megrelishvili - avatar