问题描述
让我们考虑一些综合但富有表现力的例子.假设我们有Header.h:
Let's consider some synthetic but expressive example. Suppose we have Header.h:
Header1.h
Header1.h
#include <iostream>
// Define generic version
template<typename T>
inline void Foo()
{
std::cout << "Generic\n";
}
Header2.h
Header2.h
void Function1();
Header3.h
Header3.h
void Function2();
Source1.cpp
Source1.cpp
#include "Header1.h"
#include "Header3.h"
// Define specialization 1
template<>
inline void Foo<int>()
{
std::cout << "Specialization 1\n";
}
void Function1()
{
Foo<int>();
}
后来,我或其他人在另一个源文件中定义了类似的转换.Source2.cpp
Later I or some else defines similar conversion in another source file.Source2.cpp
#include "Header1.h"
// Define specialization 2
template<>
inline void Foo<int>()
{
std::cout << "Specialization 2\n";
}
void Function2()
{
Foo<int>();
}
main.cpp
#include "Header2.h"
#include "Header3.h"
int main()
{
Function1();
Function2();
}
问题是什么将打印Function1()和Function2()?答案是不确定的行为.
The question is what will print Function1() and Function2()? The answer is undefined behavior.
我希望在输出中看到:专业1专业化2
I expect to see in output: Specialization 1Specialization 2
但是我看到:专业化2专业化2
But I see: Specialization 2Specialization 2
为什么C ++编译器对违反ODR保持沉默?在这种情况下,我希望编译失败.
Why C++ compilers are silent about ODR violation? I would prefer compilation to be failed in this case.
我只找到一种解决方法:在未命名的命名空间中定义模板函数.
I found only one workaround: define template functions in unnamed namespace.
推荐答案
编译器处于静默状态,因为 [basic.def.odr/4] :
The compiler is silent, because it's not required to emit anything by [basic.def.odr/4]:
这篇关于为什么C ++链接程序对违反ODR保持沉默?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!