0

Suppose I have a template function and it's specialization.

template<class T>
void myfunction(T whatever)
{
}

template<>
void myfunction(int whatever)
{
    //whatever
}

As far as I know, if I never call myfunction is the my programm, the template will never be instantiated and will go into the binary code only regarded as a template(correct me if I am wrong). My question is, will the specialization of myfunction be instantiated in this case?

If yes, how would it differ from the following definition of myfunction

void myfunction(int whatever)
{
    //This is the version of myfunction with the same interface as the template specialization of myfunction above, 
    //except it is not templated.
}
Karen Baghdasaryan
  • 2,407
  • 6
  • 24
  • *how would it differ?* - You cannot call the latter with `myfunction(42)`; – ph3rin Aug 19 '21 at 15:08
  • Possible/Probable duplicate? https://stackoverflow.com/q/16865225/10871073 – Adrian Mole Aug 19 '21 at 15:09
  • 1
    If you ask which will be removed from binary when not used, it's easy enough to check: https://godbolt.org/z/aeWq3rxW7. If you are asking about general differences, see [Template Specialization VS Function Overloading](https://stackoverflow.com/questions/7108033/template-specialization-vs-function-overloading) – Yksisarvinen Aug 19 '21 at 15:10
  • Well both the mentioned links answer my question, thank you. – Karen Baghdasaryan Aug 19 '21 at 15:19

0 Answers0