c++ - 内联函数选择标准

标签 c++ inline one-definition-rule

我有以下内容:

//a.cpp
inline int f(int x) { return x; }
int g(int x) { return f(x); }

//b.cpp
#include <iostream>
inline int f(int x) { return x + 1; }
extern int g(int);
int main() {
    std::cout << g(2) << f(2) << std::endl;
}

输出为22(MSVC和GCC),即a.cppf函数被内联而不是 b.cpp一个。在这种情况下选择 f 函数的标准是什么?

最佳答案

您的代码格式错误,无需诊断。所有内联函数定义在所有翻译单元中都必须相同。

由于您不这样做,代码格式错误,但允许编译并且不需要引起任何类型的警告或错误。

关于c++ - 内联函数选择标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56348580/

相关文章:

Kotlin 内联函数只有 return 语句

c# - C# 会内联这些函数吗?

c++ - 打破一个 TU 内的 ODR?

c++ - Windows 的 objcopy 等价物? (破解冲突库符号)

c++ - 在 C++ 中查找重复文件的最佳方法是什么?

JavaScript 内联事件或事后添加事件

c++ - 谁能解释一下当前C++0x标准草案的这一段?

c++ - 不同翻译单元中不可重载的非内联函数定义

c++ - boost 序列化错误 C4308 : negative integral constant converted to unsigned type

c++ - 在 C++ 中的多个 Dll 之间共享变量