The explicit specialized function of the template foo() must be visible before it can be called / initialized.
In fact, this rule applies to all Template functions.
Decision:
Move the template specialization for foo() to main() .
The following should work fine:
template <class T> T foo(T a, T b); template <> int foo<int>(int a, int b) { cout<<"int specialization"; } int main() { int x=34, y=54; cout<<foo(x, y); } template <class T> T foo(T a, T b) { return a+b; }
source share