0

Consider the code:

...
template <typename T>
void Swap(T &,T &);
template <> void Swap<structEmployee>(structEmployee &,structEmployee &);
int main()
{
template void Swap<char>(char &,char &);
short a=10,b=20;
...
Swap(a,b);
...
...
}

It is giving me the error:

expected primary-expression before ‘template’
 template void Swap<char>(char &, char &);
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
dlp96
  • 131
  • 2
  • 10

1 Answers1

0

You can't instantiate a template at block scope, it has to be in global scope:

//Instantiation in global scope
template void Swap<char>(char &,char &);

int main()
//...
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • But I'm trying for explicit instantiation and it's proper syntax is template void Swap(char&, char&). What you are talking about is `explicit specialization' and it is not the case. Kindly refer this: http://stackoverflow.com/questions/4933056/how-do-i-explicitly-instantiate-a-template-function – dlp96 Jul 25 '16 at 05:22
  • @dlp96 So sorry, didn't see that detail. I edited my answer – Rakete1111 Jul 25 '16 at 05:34
  • After instantiating it in the global scope, it is again giving me this error- `error: explicit instantiation of ‘void Swap(T&, T&) [with T = char]’ but no definition available [-fpermissive]`. I have defined the `template void Swap(T &a,T &b) {...}` after the `main` function but it is giving me this error. – dlp96 Jul 25 '16 at 06:08
  • @dlp96 Can't be sure, but I think that you have to define it *before* the instantiation – Rakete1111 Jul 25 '16 at 06:11