C++ 声明顺序(在多变量声明行中)

标签 c++ variables compilation language-lawyer declaration

我在我的 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/

相关文章:

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c - 关于ubuntu gcc编译器的问题

linux - cygwin 中 WinMain@16 下的 undefined reference

c++ - For 循环不停止

c++ - 例如,在 C++ 中的意思是 typedef void(Number::*Action)(int &);

android - cmake - 一次针对多个架构,无需在构建之间手动清理

javascript - $ 在 jQuery 中是什么意思?

c++ - 停止 QThread worker 处理即将被删除的资源的正确方法是什么?

variables - 批量添加时间分钟数(使用算术)

c++ - 尝试减小输出大小后,Visual Studio 编译程序无法正确初始化