gpt4 book ai didi

c++ - Comeau 编译器中的未命名结构

转载 作者:搜寻专家 更新时间:2023-10-31 01:13:35 24 4
gpt4 key购买 nike

为什么我们不能使用这样的东西

struct
{
int bar;
} Foo;

int main() {}

在 C++ 模式的 Comeau 编译器中,而在 C 模式下我们可以吗?

"ComeauTest.c", line 4: error: use of a type with no linkage to declare a variable with linkage } Foo; ^

最佳答案

每 3.5:8,

A type without linkage shall not be used as the type of a variable or function with external linkage unless
— the entity has C language linkage (7.5), or
— the entity is declared within an unnamed namespace (7.3.1), or
— the entity is not odr-used (3.2) or is defined in the same translation unit.

请注意,这是从 C++98 放宽的,它具有(在 3.5:8 中):

A name with no linkage [...] shall not be used to declare an entity with linkage.

这条规则的原因是为了从另一个翻译单元引用变量,您需要知道它的类型,这对于未命名的类型是不可能的。在实践中,这是因为具有外部链接的变量与其类型一起被破坏以生成明确的名称,这对于未命名的类型是不可能的。

但是,在您的代码中,实体 Foo(可以说)未被 ODR 使用,并且肯定已定义,因此它应该被符合标准的 C++11 编译器接受,Comeau 是不是。

关于c++ - Comeau 编译器中的未命名结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12355161/

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