C++:令人困惑的声明语义

发布于 2024-08-03 19:39:48 字数 326 浏览 8 评论 0原文

在尝试了 Perl 和一点点 C 之后,我正在尝试学习 C++,但我已经陷入了细节和陷阱的泥潭。考虑一下:-

int x = 1;
{ 
  int x = x; // garbage value of x
}
int const arr = 3;
{ 
  int arr[arr]; // i am told this is perfectly valid and declares an array of 3 ints !! 
} 

嗯,为什么有区别?

澄清:使用相同的名称在一种情况下有效,在另一种情况下无效。

After trying my hand at Perl and a little bit of C, I am trying to learn C++ and already i am bogged down by the details and pitfalls. Consider this:-

int x = 1;
{ 
  int x = x; // garbage value of x
}
int const arr = 3;
{ 
  int arr[arr]; // i am told this is perfectly valid and declares an array of 3 ints !! 
} 

Huh, Why the difference ?

To Clarify: Use of the same name is valid in one case and invalid in another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱世争霸 2024-08-10 19:39:48

欢迎来到 C++ 的世界!
对于您的问题,答案在于一个称为“声明点”的概念。

>>int x = 1;
>>{ int x = x; }  // garbage value of x

来自第:-3.3.1.1(C++ 标准草案)
名称的声明点紧接在其完整声明符之后和其初始值设定项(如果有)之前,除非下面另有说明。

int x = 12;
{ int x = x; }

这里; “operator =”是初始化器。您可以说尚未到达“x”的声明点,因此“x”的值是不确定的。

>>int const arr = 3;
>>{ int arr[arr]; } // i am told this is perfectly valid and declares an array of 3 ints !!

为什么?
来自第:-3.3.1.4(C++ 标准草案)
非本地名称在隐藏它的本地名称声明之前一直保持可见。这里到达声明点“;”特点。因此,使用“arr”的早期可见值,即 = 3。

另外,您可能希望知道以下内容是否有效:-

const int e = 2;
{ enum { e = e }; } // enum e = 2

来自第:-Chapter-3.3.1.4(C++ 标准草案):-
枚举数的声明点紧接在其枚举数定义之后。

但是,不要这样做,

const int Spades = 1, Clubs = 2, Hearts = 3, Diamonds = 4;
enum Suits
{
  Spades = Spades,     // error
  Clubs,               // error
  Hearts,              // error
  Diamonds             // error
};

为什么?因为枚举数被导出到枚举的封闭范围。在上面的示例中,声明了枚举器黑桃、梅花、红心和方块。因为枚举数被导出到封闭范围,所以它们被认为具有全局范围。示例中的标识符已在全局范围内定义。所以这是一个错误。

有关更多详细信息和陷阱 (:-) ),请阅读 C++ 标准草案中的第 3.3 节“声明性区域和作用域”,如果有兴趣,您可以从 此处(http://www.research.att.com/~bs/SC22-N-4411.pdf)。

Welcome to the universe of C++!
For your question, the answer lay in a concept called 'Point-of-declaration'.

>>int x = 1;
>>{ int x = x; }  // garbage value of x

From Section:-3.3.1.1 (C++ Standard Draft)
The point of declaration for a name is immediately after its complete declarator and before its initializer (if any), except as noted below.

int x = 12;
{ int x = x; }

Here; the 'operator =' is the initializer. You can say that the point-of-declaration for 'x' is not yet reached, so the value of 'x' is indeterminate.

>>int const arr = 3;
>>{ int arr[arr]; } // i am told this is perfectly valid and declares an array of 3 ints !!

Why?
From Section:-3.3.1.4 (C++ Standard Draft)
A nonlocal name remains visible up to the point of declaration of the local name that hides it. Here the point of declaration is reached at ';' character. So the earlier visible value of 'arr' is used i.e. = 3.

Also, you may wish to know that the following is valid :-

const int e = 2;
{ enum { e = e }; } // enum e = 2

From Section:-Chapter-3.3.1.4 (C++ Standard Draft):-
The point of declaration for an enumerator is immediately after its enumerator-definition.

But, don't do this

const int Spades = 1, Clubs = 2, Hearts = 3, Diamonds = 4;
enum Suits
{
  Spades = Spades,     // error
  Clubs,               // error
  Hearts,              // error
  Diamonds             // error
};

Why? Because enumerators are exported to the enclosing scope of the enumeration. In the above example, the enumerators Spades, Clubs, Hearts, and Diamonds are declared. Because the enumerators are exported to the enclosing scope, they are considered to have global scope. The identifiers in the example are already defined in global scope. So its an error.

For additional details and pitfalls ( :-) ), read-up on section 3.3 'Declarative regions and scopes' from the Draft C++ Standard, if interested you can get hold of the pdf from here(http://www.research.att.com/~bs/SC22-N-4411.pdf).

凤舞天涯 2024-08-10 19:39:48

首先,在现实世界中,你不应该使用两者,因为它很混乱,甚至花几秒钟来理解这个要点也太浪费了。

废弃我的答案的其余部分 - Abhay 已经说对了,而且更详细:)

First, in real world you should use neither because it is confusing, and even a few seconds for understanding this fine point is too much waste.

Scrap the rest of my answer - Abhay already got it right, and in far more detail :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文