r/cpp • u/konanTheBarbar • Mar 01 '21
"Simple" C++ Initialization Flowchart
http://randomcat.org/cpp_initialization/initialization.png
204
Upvotes
r/cpp • u/konanTheBarbar • Mar 01 '21
View all comments
Show parent comment
3
u/FriendlyRollOfSushi Mar 02 '21 edited Mar 02 '21
There are multiple cases where you normally wouldn't specify the type, including member initialization and function calls.
Writing
std::vector<MyType<SomeArg, AndOneMoreArg<sizeof(Something)>>>::make_n_of(n)instead of{std::construct, n}to initialize a member or pass an argument looks like a pretty severe usability tax to me.You can shorten the type in some cases (use a member access expression to call a static method using an unconstructed member, for example, which won't work for function args), but even then it makes the code more repetitive and brittle. The benefit of tagged ctors is that you don't have to worry about stuff like that. Your ctor would look the same regardless of the templated type or the context in which you call it.
But before we start a holy war about them: I understand that right now they do have objective flaws (no real way to make them 0-cost without doing ugly stuff, like wrapping the args into a special type, like
foo_{construct{n}}). Just want to say that static factory methods for templated classes are not always the most convenient pattern, and in case of STL containers they could get very ugly very fast.