简体   繁体   English

对于带有内联的 function 声明,外部是否重要?

[英]Does extern matter for function declaration with inline?

As specified in the Standard如标准中规定

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 .如果 function 的标识符声明没有存储类说明符,则其链接的确定与使用存储类说明符extern声明时完全相同。

But function specifier part gives inline function semantic as follows:但是 function 说明符部分给出了inline function 语义,如下所示:

Any function with internal linkage can be an inline function.任何具有内部链接的 function 都可以是内联 function。 For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit.对于具有外部链接的 function,适用以下限制:如果使用内联 function 说明符声明 function,则它也应在相同的翻译单元中定义。 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.如果翻译单元中 function 的所有文件 scope 声明都包含内联extern说明符,则该翻译单元中的定义是内联定义。

Case 1.情况1。

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

inline void test(void); //does it provide an external definition?

Case 2.案例 2。

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

extern inline void test(void); //does it provide an external definition?

Case 3.案例 3。

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

void test(void); //does it provide an external definition?

I have a confusion regarding the three cases.我对这三种情况感到困惑。 Are there differences between them?它们之间有区别吗? I currently think about them as我目前认为它们是

Case 1 -- does not provide an external definition ( inline without extern )案例 1 - 不提供外部定义( inline没有extern

Case 2 -- provides external definition ( inline with extern )案例 2 - 提供外部定义(与extern inline

Case 3 -- provides external definition (same as with extern )案例 3 - 提供外部定义(与extern相同)

static and extern cannot go together. staticextern不能一起使用 go。

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

inline void test(void); //does it provide an external definition?

should actually be实际上应该是

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

static inline void test(void); //does it provide an external definition?

because the definition and the declaration should match.因为定义和声明应该匹配。 I am not sure though that static actually needs to be used when also using inline .我不确定static在使用inline时实际上是否需要使用。


Case 2 -- provides external definition (inline with extern)案例 2——提供外部定义(与 extern 内联)

Case 3 -- provides external definition (same as with extern)案例 3 -- 提供外部定义(与 extern 相同)

these actually conflict (if I understand correctly) with:这些实际上与(如果我理解正确的话)冲突:

Any function with internal linkage can be an inline function.任何具有内部链接的 function 都可以是内联 function。

extern is exactly about external linkage, as opposed to internal. extern完全是关于外部链接,而不是内部链接。

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

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