【发布时间】:2019-09-23 09:01:25
【问题描述】:
考虑以下示例:
fnc.h:
#include <iostream>
template <class T> //common template definition
void fnc()
{
std::cout << "common";
}
fnc.cpp:
#include "fnc.h"
template<> //specialization for "int"
void fnc<int>()
{
std::cout << "int";
}
main.cpp
#include "fnc.h"
extern template void fnc<int>(); // compiler will not generate fnc<int> and linker will be used to find "fnc<int>" in other object files
int main()
{
fnc<double>(); //using instantiation from common template
fnc<int>(); using specialization from "fnc.cpp"
}
然后我将 extern template void fnc<int>(); 替换为 template<> void fnc<int>(); 并假设行为将相同。将extern 关键字与模板一起使用是否有任何实际意义,还是仅出于可读性而引入?
【问题讨论】:
标签: c++ templates linker instantiation extern