我在我的 C++ 代码中使用以下内容:
int a = 0, b = a;
我想知道这种行为是否可靠且定义明确(名称声明从左到右的顺序),并且我的代码不会因未声明的名称错误而与其他编译器中断。
如果不可靠,我会打破声明:
int a = 0;
int b = a;
谢谢。
最佳答案
我认为答案是否定的。
受制于core active issue 1342其中说:
It is not clear what, if anything, in the existing specification requires that the initialization of multiple init-declarators within a single declaration be performed in declaration order.
我们在[dcl.decl]p3中有不规范的说明其中说:
...[ Note: A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is
T D1, D2, ... Dn;
is usually equivalent to
T D1; T D2; ... T Dn;
...
但它是非规范的,它根本不涵盖初始化情况,据我所知,没有规范的措辞说同样的话。
尽管该标准确实涵盖了 [basic.scope.pdecl]p1 中的名称范围其中说:
The point of declaration for a name is immediately after its complete declarator and before its initializer (if any), except as noted below. [ Example:
unsigned char x = 12; { unsigned char x = x; }
Here the second x is initialized with its own (indeterminate) value. — end example ]
关于C++ 声明顺序(在多变量声明行中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698162/