gpt4 book ai didi

c++ - 是否允许 extern extern "C"和 extern "C"extern?

转载 作者:太空狗 更新时间:2023-10-29 20:19:38 28 4
gpt4 key购买 nike

这段代码是否正确?

extern "C" extern int x;       // 1
extern extern "C" int y; // 2
extern "C" extern "C" int z; // 3

int main() { }

gcc 拒绝 1 和 2 作为语法错误并接受 3。clang 接受所有这三个,但对它们全部给出重复声明说明符警告。

可能相关的是 C++17 [dcl.stc]/5:

The extern specifier can be applied only to the names of variables and functions. The extern specifier cannot be used in the declaration of class members or function parameters. For the linkage of a name declared with an extern specifier, see 6.5. [Note: The extern keyword can also be used in explicit-instantiations and linkage-specifications, but it is not a storage-class-specifier in such contexts. —end note ]

最佳答案

extern extern "C" 不是有效语法,因为 extern "C" 不是说明符,因此不能出现在 decl-specifier-seq 中。相反,extern "C" 只能作为链接规范的一部分出现,其语法是

extern string-literal { declaration-seq(opt) }
extern string-literal declaration

因此,extern "C" 必须先出现。

此外,根据 [dcl.link]/7,extern "C"extern 也是无效的:

A declaration directly contained in a linkage-specification is treated as if it contains the extern specifier (10.1.1) for the purpose of determining the linkage of the declared name and whether it is a definition. Such a declaration shall not specify a storage class.

(extern 是存储类说明符。)

虽然似乎没有任何规则禁止extern "C"extern "C"

关于c++ - 是否允许 extern extern "C"和 extern "C"extern?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518041/

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