简体   繁体   中英

function template specialization compile error

##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());

The error I get is: error LNK2005: "void __cdecl func(int)" (??$func@H@@YAXH@Z) already defined in A.obj, one or more multiply defined symbols found

Is a function template specialization not treated as a normal function template? It looks like it will be in the objective file for A.

作为template<> void func<int>(int t) {}是函数重载而不是函数模板(即,所有类型在定义时都已知,因此它不再是模板),它必须标记为inline或在.cpp文件中定义,以避免多个定义错误,就像任何其他函数定义一样。

The problem is as follows: full template specialization is no more a template , it's more like an ordinary function . So you should act accordingly:

  • either put definition of func<int>() in cpp file

  • or make it inline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM