gpt4 book ai didi

c++ - 试图理解§3.3.1/4

转载 作者:可可西里 更新时间:2023-11-01 15:24:52 26 4
gpt4 key购买 nike

<分区>

显然从 §3.3.1/4 开始,此代码段无法编译,因为它在全局命名空间 extern int A; 中包含两个具有相同名称 A 的不同实体;static int A = 101;。即一有外联,一有内联。

live example

#include <iostream>
extern int A;
static int A = 101;
class A{};
int main()
{
std::cout << A << '\n';
}

那么,为什么这段代码可以编译?

#include <iostream>
static int A = 101;
extern int A;
class A{};
int main()
{
std::cout << A << '\n';
}

编辑

我认为这个问题被接受的答案,其中这个被认为是重复的,基本上说在第二个片段中,变量 A 仍然有内部链接,尽管 extern声明。但这与我在下面对@dyp 的评论中提到的第 §3.5/4 段不一致。

§3.5/4:

An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage. A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of

— a variable; or

...

编辑 1:

OP 使用 §3.5/6 来证明他对另一个问题的回答。

§3.5/6(强调我的):

The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage.

很明显,这个答案不适用于我的问题中显示的片段,因为变量 A 的声明不是 block 范围声明。

编辑 2:

This issue状态为“就绪”的人说 §7.1.1/7 应该删除,因为它是错误的。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com