Reputation:
How to make a specialisation for a template function with returning value template?
I tried this:
myfunc.h:
#pragma once
template< >
int MyFunc<int>(){
return 10;
}
main.cpp:
#include "myfunc.h"
int main()
{
int a;
a = MyFunc<int>();
return 0;
}
but i have error: expected initializer before '<' token
Upvotes: 0
Views: 208
Reputation: 512
I don't know what are you trying to do but maybe this helps you:
template<typename T>
int MyFunc(){
return 0;
}
template<>
int MyFunc<int>(){
return 10;
}
template<>
int MyFunc<char>(){
return 100;
}
using namespace std;
int main()
{
cout << MyFunc<int>() << endl << MyFunc<char>() << endl;
system("pause");
return 0;
}
Upvotes: 0
Reputation: 146930
You did not declare or define a primary template of which this is a specialization.
Upvotes: 2
Reputation: 59811
You are missing the primary template before you declare your specialization.
template<typename> int func() { return 42; }
template<> int func<int>() { return 23; }
Please be aware of the problems of function specializations.
Upvotes: 4