假设我有两个 .cpp 文件 file1.cpp
和 file2.cpp
:
// file1.cpp
#include <iostream>
inline void foo()
{
std::cout << "f1\n";
}
void f1()
{
foo();
}
和
// file2.cpp
#include <iostream>
inline void foo()
{
std::cout << "f2\n";
}
void f2()
{
foo();
}
并且在 main.cpp
我已经转发声明了 f1()
和 f2()
:
void f1();
void f2();
int main()
{
f1();
f2();
}
结果(不依赖于构建,调试/发布构建的结果相同):
f1
f1
哇:编译器不知何故只从 file1.cpp
中挑选定义并在 f2()
中使用它。这种行为的确切解释是什么?
注意,将 inline
更改为 static
是解决此问题的方法。将内联定义放在未命名的命名空间中也可以解决问题,程序会打印:
f1
f2
最佳答案
这是未定义的行为,因为具有外部链接的同一内联函数的两个定义打破了 C++ 对可以在多个位置定义的对象的要求,称为一个定义规则:
3.2 One definition rule
...
- There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14),[...] in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then
6.1 each definition of D shall consist of the same sequence of tokens; [...]
这不是 static
函数的问题,因为一个定义规则不适用于它们:C++ 认为在不同翻译单元中定义的 static
函数独立于每个其他。
关于c++ - 为什么在 2 个不同的 cpp 文件中定义内联全局函数会导致神奇的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43736349/