我有以下内容:
//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.cpp
f
函数被内联而不是 b.cpp
一个。在这种情况下选择 f
函数的标准是什么?
最佳答案
您的代码格式错误,无需诊断。所有内联函数定义在所有翻译单元中都必须相同。
由于您不这样做,代码格式错误,但允许编译并且不需要引起任何类型的警告或错误。
关于c++ - 内联函数选择标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56348580/