0

Class declaration is in a class.h file :

template <typename K, typename T>
class classx
{
  ...
  unsigned int func1(K key);
  ...

which includes this class.hpp :

template <typename K, typename T>
unsigned int classx<K,T>::func1(K key)
{
    return 1;
}


//Func1 for <int, typename T>       ????

template <>
template <typename T>
unsigned int classx<int,T>::func1<int, T>(int key)  // ERROR!
{
    return 1;
}

This results:

error: expected initializer before ‘<’ token

What is the proper way of doing this?

vkx
  • 424
  • 1
  • 7
  • 17

1 Answers1

0

Remove the second set of template parameters from your func1 definition and the template <>:

template <typename T>
unsigned int classx<int,T>::func1(int key)
{
    return 1;
}

EDIT:

Additionally, you cannot partially specialize a single function of a template class. If you wish to do so, you will have to partially specialize the entire class.

Community
  • 1
  • 1
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
  • Still does not compile : error: invalid use of incomplete type ‘class classx – vkx Jun 05 '16 at 00:03
  • @vkx - "Invalid use of incomplete type" is commonly caused by attempting to use a class without including the header file (e.g. https://stackoverflow.com/questions/5543331/invalid-use-of-incomplete-type-struct-even-with-forward-declaration). Simply forward declaring a class before using it isn't sufficient. – Karl Nicoll Jun 05 '16 at 00:07
  • @vkx - Ah okay I see your point. Unfortunately you can't partially specialize a template function, [you have to partially specialize the entire class](http://stackoverflow.com/a/165153/52724). – Karl Nicoll Jun 05 '16 at 00:32
  • Thank you. If you edit your answer with this comment, I can accept – vkx Jun 05 '16 at 00:40
  • @vkx - Glad I could help! – Karl Nicoll Jun 05 '16 at 00:45