- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这个问题是关于代码的(在命名空间范围内):
std::vector<int> v1;
std::vector<int> v2(4);
在 C++14 (N4140) 的 3.6.2 节中定义了一个术语常量初始化:
Constant initialization is performed:
- [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.
此外,constant initializer 一词刚刚在前面定义:
A constant initializer for an object
o
is an expression that is a constant expression, except that it may also invoke constexpr constructors foro
and its subobjects even if those objects are of non-literal class types.
因此,查看 std::vector<int> v2(4)
.
此对象由构造函数调用初始化,因此它包含在第二个要点中。初始化全表达式是4
. 4
是一个常量表达式,因此它是一个常量初始化器。所以第二个要点得到满足,这应该是常量初始化的情况。
然而,我测试了几个编译器,它们似乎都处理 v2
作为动态初始化。
对于 v1
情况下,不清楚这是否算作“由构造函数调用初始化”;如果是这样,初始化完整表达式会是什么。
我的问题是:是 v1
和 v2
常量初始化,或者动态初始化;如果是后者,是否可以解释标准中的这些引用是如何解释的?
最佳答案
根据 1.9/10,初始化的完整表达式包括对构造函数的调用:
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
使用)涉及构造函数调用。
因此,根据您引用的第二个要点,构造函数调用是“初始化完整表达式”的一部分。所以构造函数调用,而不仅仅是 4
,是完整的表达式。这反过来意味着这样的构造函数必须是 constexpr
才有资格作为 constant initializer(根据它的定义,正如你引用的那样)。由于 std::vector
的默认构造函数和 size_t
构造函数都不是 constexpr
,因此初始化不是常量。
关于c++ - 这些 vector 定义是 "constant initialization"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131331/
所以如果我在 C++ 中有这样的东西: char A_char = 'A'; char * myPtr = &A_char; const char * myPtr = &char_A; //point
我试图在我的 Perl 脚本中将魔数(Magic Number)声明为常量,如 perlsub 中所述。但是,我收到警告: $ cat foo.perl use warnings ; use stri
我想为 data Constant a b = Constant a 这是我的直接尝试: instance Foldable (Constant a) where foldr f b (Const
我在客户端和服务器端拆分了我的文件夹,但我没有从父文件夹工作,我表现得好像它们是 2 个不同的文件夹...现在我想部署到 Heroku 但我为此需要一个主文件夹,所以我想更改我的 webpack.co
当函数不修改对象参数时,我总是让它请求一个常量引用,即使引用的对象不是真正的常量。这是错误的吗? 对于包装类,我想这样写: template class Wrapper{ private: B*
核心常量表达式的定义取决于常量表达式的概念,如要点 (2.7.1) 和 (2.9.1) 所示N4140 的。 §5.19/2: A conditional-expression e is a core
我有以下代码片段,它按预期工作。其中 x 是一个变量 var myVariable = (x === 'A' || x=== 'B') ? 'sui' : 'pai'; 但是闭包编译器正在将它转换为
我是一个国际化应用程序。其中一部分在于菜单的国际化。没关系。 通过 GWT,我可以使用 Constants 接口(interface)。 现在我必须国际化该应用程序的帮助,其中包括一些涉及菜单的文本。
在 Bjarne Stroustrup 的 A Tour of C++ 中,每章末尾都列出了一些建议。在第一章的结尾,其中一个写道: Avoid ‘‘magic constants;’’ use sy
创建常量数组的常量数组的语法是什么? 我希望函数参数是常量 char* 字符串的常量数组。 最佳答案 您可以通过将 const 放在第一个星号的右侧来实现,例如 void f(const char *
我有一本带图书馆的 Chef Recipe ,例如库.rb。它包含一个 CONSTANT: CONSTANT = 'constant' 当我为这本 Recipe 编写单元测试时,它总是给我警告: (S
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to check for equals? (0 == i) or (i == 0) Why does
我有以下代码: constexpr unsigned long long power_function(const unsigned long long prime, const unsigned l
在一个页面上,我有几个 Angular 模块。我为每个模块定义了一个包含模块版本的常量。 var module1 = angular.module('module1').constant('versi
C++14 中的§5.19/3 定义了一个整型常量表达式和一个转换后的常量表达式: An integral constant expression is an expression of integr
如果您打开 R# 选项并转到代码编辑> C# > 命名样式,则有 2 个设置与我非常相似。本地常量和常量字段(私有(private))。一种是 lowerCaseCamel,另一种是 UpperCam
如何将恰好命名为 reverse 的以下方法重写为允许任何枚举类型的泛型方法。 public class TestX { enum Gender { male, female } pu
我和我的一位队友进行了一次有趣的谈话。 CONSTANT.equals(VARIABLE) 是否比 Java 中的 VARIABLE.equals(CONSTANT) 快? 我怀疑这是一个虚假陈述。但
我想在 c 程序中执行脚本 cmd,所以函数 SYSTEM(CONST CHAR) 可以执行它,但我想使用这个函数和 3 个不同的参数写入一次。谁能帮帮我,有没有那种功能。 最佳答案 如果我猜对了,您
VStudio 或 ReSharper 给我以下建议: constant 在这种情况下意味着什么?如果它是当前方法作用域中的一个常量,目的是什么?方法往往很小,因此与常规 var 相比,它不应该有任何
我是一名优秀的程序员,十分优秀!