C ++声明顺序(在多变量声明行中) [英] C++ Order of Declaration (in Multi-variable Declaration Line)

查看:138
本文介绍了C ++声明顺序(在多变量声明行中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++代码中使用以下内容:

int a = 0, b = a;

我想知道这种行为是否可靠并且定义正确(名称声明的从左到右顺序),并且我的代码不会与其他编译器一起出现,并出现未声明名称错误. >

如果不可靠,我会破坏声明:

int a = 0; 
int b = a;

谢谢.

解决方案

我相信答案是否定的.

它受核心活动问题1342 表示:

尚不清楚现有规范中有哪些要求(如果有的话)要求以声明顺序执行单个声明中的多个init声明符的初始化.

我们在 [dcl.decl] p3

... [注:通常有多个声明符的声明 等价于相应的声明序列,每个声明序列都带有一个 单个声明符.那是

T D1, D2, ... Dn;

通常等同于

T D1; T D2; ... T Dn;

...

但是它是非规范性的,它根本不涵盖初始化情况,据我所知,规范性措辞没有说同一件事.

尽管该标准确实涵盖了 [basic.scope .pdecl] p1 表示:

名称的声明要在名称完成后立即进行 声明符,并在其初始值设定项(如果有)之前,除非另有说明. [示例:

unsigned char x = 12;
{ unsigned char x = x; }

在此,第二个x用其自己的(不确定的)值初始化. —示例example]

I use the following in my C++ code:

int a = 0, b = a;

I would like to know if this behaviour is reliable and well defined (left to right order of name declaration) and that my code will not break with other compilers with an undeclared name error.

If not reliable, I would break the statement:

int a = 0; 
int b = a;

Thank you.

解决方案

I believe the answer is no.

It is subject to core active issue 1342 which says:

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.

We have non-normative note in [dcl.decl]p3 which says:

...[ 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;

...

but it is non-normative and it does not cover the initialization case at all and as far as I can tell no normative wording says the same thing.

Although the standard does cover the scope of names in [basic.scope.pdecl]p1 which says:

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 ++声明顺序(在多变量声明行中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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