Are these vector definitions “constant initialization”?

雨燕双飞 提交于 2019-12-06 06:22:21

As per 1.9/10, the full expression of an initialization includes the call to the constructor:

A full-expression is an expression that is not a subexpression of another expression. ... If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition. ... Conversions applied to the result of an expression in order to satisfy the requirements of the language construct in which the expression appears are also considered to be part of the full-expression.
[ Example:
...

S s1(1); // full-expression is call of S::S(int)

Then, per 8.5/17, direct initialisation (which both v1 and v2 use) involves a constructor call.

Therefore, the constructor call is part of the "initialization full-expression" per the 2nd bullet point you quote. So the constructor call, and not just the 4, is the full-expression. This in turn means that such a constructor must be constexpr to qualify as a constant initializer (per its definition, as you quoted). And since neither the default constructor nor the size_t constructor of std::vector are constexpr, the initialisation is not constant.

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