4 ms·
> It's the same as std::string product; product = "not worked"; Not a C++ person, so please forgive my ignorance, but what is the difference between the above
by kleiba 5y ago
> It's the same as std::string product; product = "not worked";
Not a C++ person, so please forgive my ignorance, but what is the difference between the above and sth. like:
std::string product = "not worked";
or
std::string product("not worked");
(Are these even legal C++ statements and, if not, why not? ;-))
- MakersF 5y agoIn practical terms, none. They are the same, and the same as using the curly braces. From the POV of the standard, those are different kinds of initializations. C++ has like 12 different ways of initializing variables, and the differences are quite confusing, but in practical terms in my experience I never had to care too much beside making sure native types are initialized to some specified value. You can probably find some talks on YouTube talking about the initializations of c++, and 1h30m is probably not enought to cover all the details :')
- kleiba 5y agoYeah, that sounds like C++, I suppose. Thanks very much.
- klibertp 5y agoBoth 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...
- dmitrykoval 5y agoBoth are legal statements. The second one is a direct initialization, invoking corresponding constructor. The first one first invokes default constructor and then copy or move assignment depending on rvalueness of the arg.
- kleiba 5y agoThanks very much for the explanations, folks! One more follow-up question: what are reasons to prefer: std::string product; product = "not worked"; i.e., separate declaration and initialization, as suggested by the grand-parent?
- dmitrykoval 5y agoFor this particular example direct initialization i.e. std::string product("not worked"); would be preferred, as you end up using one call to constructor instead of two: default constructor followed by the move assignment. https://en.cppreference.com/w/cpp/language/direct_initialization https://en.cppreference.com/w/cpp/language/direct_initializa... https://en.cppreference.com/w/cpp/language/move_assignment https://en.cppreference.com/w/cpp/language/move_assignment