1

Consider the following code:

class Bar;

enum Values
{
  ValA,
  ValB, // ...
};

template< typename T >
struct Foo : virtual public Bar
{
};

template<>
struct Foo< ValA >
{
  void doSomething();
};

If I define the implementation of doSomething() in the header file, the program doesn't complain when I use Foo::doSomething(). However, if I move the implementation to a cpp file, as follows, I get an error:

template<>
void Foo< ValA >::doSomething()
{
  // ...
}

The error:

error: template-id 'doSomething<>' for 'void Foo<(Values)0u>::doSomething()' does not match any template declaration.

I'm not sure why this is failing. Moving the specialized implementation to a cpp file should not be a problem, I believe. I've done this before.

MarkP
  • 4,168
  • 10
  • 43
  • 84
  • Take a look here? http://stackoverflow.com/questions/13034799/define-template-specialization-in-cpp?rq=1 – yizzlez May 07 '14 at 20:43
  • `Foo< ValA >` is not a template. It's (just) a class. Drop the `template<>`. – dyp May 07 '14 at 20:53

1 Answers1

1

There are couple of problems.

One was pointed out by @dyp in the comments section to your question. You need to use:

void Foo< ValA >::doSomething()
{
}

instead of

template<>
void Foo< ValA >::doSomething()
{
}

The other one is that you have to change the class template to:

template< int T >
struct Foo : virtual public Bar
{
};

You can't create a specialization of Foo using ValA, if you use typename as template parameter.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Why don't you use the enum as the type of the non-type template parameter? – dyp May 07 '14 at 23:51
  • Cool. thanks for you answer. Using the `typename` keyword was a typo. I did mean to use the enum's name instead. – MarkP May 09 '14 at 02:32