hid*_*yat 19 c++ templates template-specialization
##A.hh
template<class T> void func(T t) {}
template<> void func<int>(int t) {}
void func2();
##A.cpp
void func2() {}
##main.cpp
func("hello");
func(int());
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:错误LNK2005:"void __cdecl func(int)"(?? $ func @ H @@ YAXH @ Z)已在A.obj中定义,找到一个或多个多重定义的符号
函数模板特化是否不被视为普通函数模板?看起来它将在A的目标文件中.
ild*_*arn 32
由于template<> void func<int>(int t) {}是函数重载而不是函数模板(即,所有类型在定义时都已知,因此它不再是模板),它必须inline在.cpp文件中标记为或定义,以避免多个定义错误,与任何其他功能定义一样.
| 归档时间: |
|
| 查看次数: |
6416 次 |
| 最近记录: |