function template specialization compile error

前端 未结 2 1989
小蘑菇
小蘑菇 2021-02-12 10:06
##A.hh

template void func(T t) {}
template<> void func(int t) {}

void func2();

##A.cpp

void func2() {}

##main.cpp

func(\"hello\");
         


        
相关标签:
2条回答
  • 2021-02-12 10:09

    As template<> void func<int>(int t) {} is a function overload rather than a function template (i.e., all types are known at the point of definition so it is no longer a template), it must be marked as inline or defined in a .cpp file to avoid multiple definition errors, just as with any other function definition.

    0 讨论(0)
  • 2021-02-12 10:11

    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

    0 讨论(0)
提交回复
热议问题