是否允许 extern extern "C" 和 extern "C" extern?

Are extern extern "C", and extern "C" extern, allowed?

此代码正确吗?

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" 必须先出现。

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

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 是存储 class 说明符。)

虽然好像没有禁止extern "C" extern "C"的规定