gpt4 book ai didi

c - extern 对于内联函数声明是否重要?

转载 作者:太空宇宙 更新时间:2023-11-04 07:43:51 24 4
gpt4 key购买 nike

按照标准中的规定

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.

但是函数说明符部分给出了 inline 函数语义如下:

Any function with internal linkage can be an inline 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. 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.

案例 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?

案例 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?

案例 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?

我对这三种情况感到困惑。它们之间有区别吗?我目前认为它们是

情况一——不提供外部定义(inline without extern)

Case 2 -- 提供外部定义(inline with extern)

案例 3 -- 提供外部定义(与 extern 相同)

最佳答案

staticextern 不能放在一起。

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?

实际上应该是

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?

因为定义和声明应该匹配。我不确定在使用 inline 时是否真的需要使用 static


Case 2 -- provides external definition (inline with extern)

Case 3 -- provides external definition (same as with extern)

这些实际上与(如果我理解正确的话)冲突:

Any function with internal linkage can be an inline function.

extern 完全是关于外部链接,而不是内部链接。

关于c - extern 对于内联函数声明是否重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58444503/

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