4 ms·
Both are valid, and were the only ways of initializing objects before C++11, I think. Brace initialization has an advantage in that it allows you to initialize
by klibertp 5y ago
Both are valid, and were the only ways of initializing objects before C++11, I think.
Brace initialization has an advantage in that it allows you to initialize compound values, like containers and structs, even if they're nested:
std::map<int, std::string> m = { // nested list-initialization
{1, "a"},
{2, {'a', 'b', 'c'} },
{3, s1}
};
Ref: https://en.cppreference.com/w/cpp/language/list_initialization https://en.cppreference.com/w/cpp/language/list_initializati... https://en.cppreference.com/w/cpp/language/aggregate_initialization https://en.cppreference.com/w/cpp/language/aggregate_initial...
- kleiba 5y agoI see, that's interesting.
- im3w1l 5y agoHow compile-time expensive is this? I guess it has to potentially consider a lot of constructor combinations?
- mellery451 5y agoit's also generally preferred because it can avoid certain narrowing conversions in construction: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-construct https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...