What is the rationale for the way C++ handles uniform initialization with initialization lists?

為{幸葍}努か 提交于 2020-02-24 14:04:44

问题


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 if the_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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!