2

I have a templated class foo which has templated member function. I have a problem with the specialization of the templated function.

template<typename T, int x...>
class foo_c
{
    ...
    template<int n>
    int foo() { return n; }
};
template<>
template<>
int foo_c<int,1>::foo<0>() { return 0; }

the above code compile but I need foo_c to be generic, when add anything to the template I get a compiler error.

Questions:

  • how can I fix this?
  • what are the rules that the compiler follow when exposed to specialization?

EDIT:

Trying

template<typename T, int x, int y>
template<>
int foo_c<T,x,y>::foo<0>() { return 0; }

Error

error: invalid explicit specialization before '>' token
error: enclosing class templates are not explicitly specialized
error: expected initializer before '<' token
CroMagnon
  • 1,218
  • 7
  • 20
  • 32
chedy najjar
  • 631
  • 7
  • 19
  • 1
    Please provide a [mcve]. What is the error you get when you "add anything to the template" – Barry Feb 09 '17 at 17:58
  • 1
    I've edited an mcve since I'm also interested in an answer. – YSC Feb 09 '17 at 18:07
  • @YSC I rolled back your edit since OP's original implementation of `foo` is correct, so I'd like OP to provide whatever error they're actually having. – Barry Feb 09 '17 at 18:11
  • @chedynajjar Ignore YSC. Add your error and example. – Barry Feb 09 '17 at 18:12
  • @Barry I fear the error message would help less than a concise & precise question; but I could be wrong. Will see. – YSC Feb 09 '17 at 18:13
  • Related to [specialization-of-templated-member-function-in-templated-class](http://stackoverflow.com/questions/6773302/specialization-of-templated-member-function-in-templated-class). – Jarod42 Feb 09 '17 at 18:18
  • 1
    Fully specialize the method (and class) is possible, but not partial specialization. – Jarod42 Feb 09 '17 at 18:20
  • @YSC That is why I asked for an MCVE from OP - to get a concise and precise question. Now that we have one, it is answerable - in this case from the dupe. – Barry Feb 09 '17 at 18:21
  • @Barry My bad; thanks for the found dup. – YSC Feb 09 '17 at 18:31

0 Answers0