简体   繁体   English

在定义和声明之间混合使用 static、extern 和 inline 关键字

[英]Mixing the usage of static, extern and inline keywords between definition and declaration

inline void t1();
void t1(){}        // t1 is still inline?

void t2();
inline void t2(){} // t2 is inline?

static void t3(){}
inline void t3(); // t3 is still static?

inline void t4();
static void t4(){} // t4 is still inline?
                   // "inline function 't4' declared but never defined", are these not the same function?

extern void t5();
inline void t5(){} // t5 is still extern?

inline void t6(){}
extern void t6(); // t6 is still inline?

int main(){
 return 0;
}

Please note in the above snippet some functions are first fully defined and their prototype is changed afterwords with a different storage type, or inlined.请注意,在上面的代码片段中,首先完全定义了一些函数,然后使用不同的存储类型或内联更改了它们的原型。

I have the following questions:我有以下问题:

1- Are the storage types, or the inline state of the functions persistent as per comments? 1- 存储类型或函数的inline状态是否根据注释保持不变?

2- Why compiler warns that t4 is declared but never defined? 2- 为什么编译器警告t4已声明但从未定义? isn't static void t4(){} the definition?不是static void t4(){}定义吗?

C 2018 6.7.4 7 says: C 2018 6.7.4 7 说:

… For a function with external linkage, the following restrictions apply: … If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern , then the definition in that translation unit is an inline definition . ... 对于具有外部链接的函数,适用以下限制: ... 如果翻译单元中函数的所有文件范围声明都包含不带externinline函数说明符,则该翻译单元中的定义内联定义

6.9 5 says: 6.9 5 说:

An external definition is an external declaration that is also a definition of a function (other than an inline definition) or an object.外部定义是一个外部声明,它也是一个函数(内联定义除外)或对象的定义。

For each of your cases except t3 , the identifier appears with external linkage and it is not true that all of the file scope declarations include inline .对于除t3之外的每个案例,标识符都与外部链接一起出现,并且并非所有文件范围声明都包含inline Therefore, there are no inline definitions for those cases.因此,这些情况没有内联定义。

inline void t1(); void t1(){} // t1 is still inline?

There is no inline definition, per above.上面没有内联定义。

void t2(); inline void t2(){} // t2 is inline?

There is no inline definition, per above.上面没有内联定义。

static void t3(){} inline void t3(); // t3 is still static?

C 2018 6.2.2 5 says: C 2018 6.2.2 5 说:

If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern如果函数的标识符声明没有存储类说明符,则其链接的确定与使用存储类说明符extern声明的完全相同……

Therefore, for inline void t3();因此,对于inline void t3(); , we may read extern inline void t3(); ,我们可以读取extern inline void t3(); . . Then 6.2.2 4 says:然后 6.2.2 4 说:

For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration…对于在该标识符的先前声明可见的范围内使用存储类说明符 extern 声明的标识符,如果先前声明指定内部或外部链接,则后面声明中标识符的链接与链接相同在事先声明中指定……

Therefore, the linkage of t3 in extern inline void t3();因此, extern inline void t3();t3的链接extern inline void t3(); is the same as in the prior declaration static void t3(){} , so it is internal.与先前声明static void t3(){} ,因此它是内部的。

inline void t4(); static void t4(){} // t4 is still inline? // "inline function 't4' declared but never defined", are these not the same function?

Per above, there is no inline definition of t4 .如上所述,没有t4内联定义。

Further, inline void t4();此外, inline void t4(); declares t4 with external linkage, and static void t4(){} declares it with internal linkage, and 6.2.2 7 says:用外部链接声明t4static void t4(){}用内部链接声明它,6.2.2 7 说:

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.如果在翻译单元内,相同的标识符出现在内部和外部链接中,则行为未定义。

extern void t5(); inline void t5(){} // t5 is still extern?

Yes, per 6.2.2 5, above.是的,根据以上 6.2.2 5。

inline void t6(){} extern void t6(); // t6 is still inline?

There is no inline definition, per above.上面没有内联定义。 This case is equivalent to fahr in example 1 in C 2018 6.7.4 10, where the commentary explicitly says that the definition of the function is an external definition.这种情况相当于 C 2018 6.7.4 10 中示例 1 中的fahr ,其中注释明确表示函数的定义是外部定义。

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

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