c++ - 为什么 C++ 链接器对 ODR 违规保持沉默?

标签 c++ templates one-definition-rule

让我们考虑一些综合但富有表现力的例子。假设我们有 Header.h:

Header1.h

#include <iostream>

// Define generic version
template<typename T>
inline void Foo()
{
    std::cout << "Generic\n";
}

Header2.h

void Function1();

Header3.h

void Function2();

源1.cpp

#include "Header1.h"
#include "Header3.h"

// Define specialization 1
template<>
inline void Foo<int>()
{
    std::cout << "Specialization 1\n";
}

void Function1()
{
    Foo<int>();
}

后来我或其他人在另一个源文件中定义了类似的转换。 源2.cpp

#include "Header1.h"

// Define specialization 2
template<>
inline void Foo<int>()
{
    std::cout << "Specialization 2\n";
}

void Function2()
{
    Foo<int>();
}

主要.cpp

#include "Header2.h"
#include "Header3.h"

int main()
{
    Function1();
    Function2();
}

问题是什么将打印 Function1() 和 Function2()?答案是未定义的行为。

我希望在输出中看到: 特化1 特化2

但我看到: 专精2 特化2

为什么 C++ 编译器对 ODR 违规保持沉默?在这种情况下,我宁愿编译失败。

我只找到一种解决方法:在未命名的命名空间中定义模板函数。

最佳答案

编译器是静默的,因为它不是要求通过[basic.def.odr/4]发出任何东西:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see [class.ctor], [class.dtor] and [class.copy]). An inline function or variable shall be defined in every translation unit in which it is odr-used outside of a discarded statement.

关于c++ - 为什么 C++ 链接器对 ODR 违规保持沉默?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45120323/

相关文章:

c++ - 不同的函数有不同的地址吗?

c++ - 执行静态对象的析构函数时崩溃

c++ - 纯虚函数实现

c++ - 为什么我在读取二进制文本时会得到 unicodeand 大数字?

c++ - 库的 .h 文件中出现 "{return 0;}"或 "{;}"的原因

c++ - c++ 模板元编程是函数式编程的一种形式吗

c++ - 通用成员函数指针作为另一个类中的模板参数

c++ - 模板方法 enable_if 特化

c++ - 如何使用 RegOpenKeyEx 将值存储到字符串?

c++ - 每次我将我的(播放旋律)代码上传到 arduino 并按下按钮时,它都会跳过数组中的相同音符。怎么会?