这些向量定义是“常量初始化”吗? [英] Are these vector definitions "constant initialization"?

查看:206
本文介绍了这些向量定义是“常量初始化”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是关于代码(在命名空间范围):

This question is about the code (at namespace scope):

std::vector<int> v1;
std::vector<int> v2(4);

在C ++ 14(N4140)的3.6.2节中定义了一个术语常规初始化

In section 3.6.2 of C++14 (N4140) there is defined a term Constant initialization:


执行常规初始化:

Constant initialization is performed:


  • 如果具有静态或线程存储持续时间的对象通过构造函数调用进行初始化,并且初始化full-expression是常量,则$ b
  • 对象的初始化;
  • 如果具有静态或线程存储持续时间的对象未通过构造函数调用初始化,并且如果对象是值初始化的或每个完全表达式出现在其对象中initializer是一个常数表达式。

  • [omitted - about reference initialization]
  • if an object with static or thread storage duration is initialized by a constructor call, and if the initialization full-expression is a constant initializer for the object;
  • if an object with static or thread storage duration is not initialized by a constructor call and if either the object is value-initialized or every full-expression that appears in its initializer is a constant expression.

是早先定义的:


一个对象的常量初始值 o 是一个
常量表达式的表达式,除了它也可以调用 o 及其子对象的constexpr构造函数,即使这些对象是非-ltyal类类型。

A constant initializer for an object o is an expression that is a constant expression, except that it may also invoke constexpr constructors for o and its subobjects even if those objects are of non-literal class types.

因此,查看 std :: vector< int& v2(4)

此对象由构造函数调用初始化,因此被第二个项目符号覆盖。初始化full-expression是 4 4 是一个常数表达式,因此它是一个常数初始值。所以第二个要点是满意的,这应该是一个常量初始化的情况。

This object is initialized by a constructor call so it's covered by the second bullet point. The initialization full-expression is 4. 4 is a constant expression, therefore it is a constant initializer . So the second bullet point is satisfied and this should be a case of constant initialization.

然而,我测试了几个编译器和所有他们似乎将 v2 视为动态初始化

However, I tested with a few compilers and all of them seem to treat v2 as dynamic initialization.

对于 v1 case,不清楚这是否为由构造函数调用初始化;

For the v1 case, it's not clear whether this counts as "initialized by a constructor call"; and if so, what the initialization full-expression would be.

我的问题是: v1 c $ c>和 v2 常量初始化动态初始化;

My question is: are v1 and v2 constant initialization, or dynamic initialization; and if the latter, could it be explained how these quotes from the standard are meant to be interpreted?

推荐答案

根据1.9版本,如果是后者,可以解释标准中的这些引用是如何解释的? 10,初始化的完整表达式包括对构造函数的调用:

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)


然后,根据8.5 / 17,直接初始化( v1 v2 )都涉及构造函数调用。

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

因此,构造函数调用是您引用的第二个项目符号的初始化全表达式的一部分。所以构造函数调用,而不仅仅是 4 ,是完全表达式。这反过来意味着这样的构造函数必须 constexpr 才能作为一个常量初始化器(按照它的定义,如你所引用的)。并且因为 std :: vector 的默认构造函数和 size_t 构造函数都不是 constexpr ,初始化不是常数。

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.

这篇关于这些向量定义是“常量初始化”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆