.cpp 文件中的模板专业化 + .h 文件中的主模板声明

use*_*113 6 c++ templates template-specialization language-lawyer

根据https://eel.is/c++draft/temp.expl.spec#7

如果模板、成员模板或类模板的成员被显式特化,则在每个翻译单元中,该特化的声明应可从该特化的每次使用中到达,这将导致隐式实例化的发生。使用发生;无需诊断。

因此我想知道,以下程序是否格式错误,NDR?

// foo.h
template <typename T>
void foo();

// Specialization not declared in the header!

// foo.cpp
#include "foo.h"

template <>
void foo<int>()
{
 // ...
}

// main.cpp
#include "foo.h"

int main()
{
    foo<int>();
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*ves 1

看起来这个确切的案例包含在标准委员会的“已关闭问题列表”中,您可以在此处阅读,很难获得比这更权威的信息。

长话短说:

理由(2016 年 3 月):

如分析中所述,目的是使示例格式错误,无需诊断。

请注意,您可以找到很多与此非常相似的问题。例如,我找到了这个,其中一位回答者引用了工作组的讨论。