问题
C++'s uniform initialization syntax fixes the most vexing parse. Yay. But then it introduces confusion when dealing with initialization lists. Boo. The existing behavior is that:
std::vector<int> the_vec{4};
will invoke std::vector(std::initializer_list<T> init)
instead of std::vector(size_type count);
.
What is the rationale for this decision? Especially since the language committee was inventing new syntax, it seems to me the other possible designs could have been:
Require initialization list constructors be invoked by doing
std::vector the_vec = { 4 };
. Granted, this is no longer "uniform", but it seems conceptually simpler to me to require that all initialization list usage use one form, especially when that form matches aggregate initialization of objects in C.Require initialization list constructors be invoked by doing
std::vector the_vec{{4}};
. This also seems conceptually simpler to me.Require initialization list constructors be invoked by doing
std::vector the_vec{{4}};
only ifthe_vec{4}
would invoke some other constructor. I don't like this as much since it's hard to tell what constructor is being invoked at the callsite, but at least it would have meant that the old constructor syntax would no longer be necessary.
来源:https://stackoverflow.com/questions/49230097/what-is-the-rationale-for-the-way-c-handles-uniform-initialization-with-initia