简体   繁体   English

允许的是extern extern“C”和extern“C”extern吗?

[英]Are extern extern “C”, and extern “C” extern, allowed?

Is this code correct? 这段代码是否正确?

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

int main() { }

gcc rejects 1 and 2 as syntax errors and accepts 3. clang accepts all three but gives a duplicate-declaration-specifier warning about them all. gcc拒绝1和2作为语法错误并接受3. clang接受所有三个但是给出一个重复声明说明符警告它们全部。

Maybe relevant is C++17 [dcl.stc]/5: 也许相关的是C ++ 17 [dcl.stc] / 5:

The extern specifier can be applied only to the names of variables and functions. extern说明符只能应用于变量和函数的名称。 The extern specifier cannot be used in the declaration of class members or function parameters. extern说明符不能用于类成员或函数参数的声明。 For the linkage of a name declared with an extern specifier, see 6.5. 有关使用extern说明符声明的名称的链接,请参见6.5。 [Note: The extern keyword can also be used in explicit-instantiation s and linkage-specification s, but it is not a storage-class-specifier in such contexts. [注意: extern关键字也可以用于显式实例化链接规范 ,但它不是这种上下文中的存储类说明符 —end note ] - 尾注]

extern extern "C" is not valid syntax, as extern "C" is not a specifier and thus cannot occur in a decl-specifier-seq . extern extern "C"是无效的语法,因为extern "C"不是说明符,因此不能出现在decl-specifier-seq中 Instead extern "C" can only appear as part of a linkage-specification , for which the syntax is 相反, extern "C"只能作为语法所在的链接规范的一部分出现

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

Thus, extern "C" must occur first. 因此, extern "C"必须首先出现。

Also, extern "C" extern is not valid either, according to [dcl.link]/7: 另外,根据[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. 直接包含在linkage-specification中的声明被视为包含extern说明符(10.1.1),用于确定声明的名称的链接以及它是否是定义。 Such a declaration shall not specify a storage class. 此类声明不应指定存储类。

( extern is a storage class specifier.) extern是存储类说明符。)

It doesn't seem that there is any rule forbidding extern "C" extern "C" , though. 但是,似乎没有任何规则禁止extern "C" extern "C"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM