其他分享
首页 > 其他分享> > 为什么C链接器对ODR违规没有提及?

为什么C链接器对ODR违规没有提及?

作者:互联网

让我们考虑一些合成但富有表现力的例子.假设我们有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();

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

#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()?答案是未定义的行为.

我希望在输出中看到:
专业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.

标签:one-definition-rule,c,templates
来源: https://codeday.me/bug/20190727/1551593.html