Defining template method specialization outside of template class definition

M

Michael Stembera

I have a case of a very simple template class w/ a
template method. If I define a specialization of the
method outside the body of the template class it does not
compile. Here is a tiny example to illustrate.


template<typename T>
class C
{
public:
template<int N> bool foo(void);
template<> bool foo<1>(void);
};

template<typename T>
template<int N>
bool C<T>::foo(void)
{
return N > 0;
}

template<typename T>
template<>
bool C<T>::foo<1>(void)
{
return false;
}
// error C2768: 'C<T>::foo' : illegal use of explicit
template arguments


However, if I define the specialization inside

template<typename T>
class C
{
public:

template<int N> bool foo(void)
{
return N > 0;
}

template<> bool foo<1>(void)
{
return false;
}
};

it compiles fine. BTW, the non specialized version can
be defined inside or outside of the class and it makes no
difference either way. Also, if the class itself is NOT
a template class both versions compile fine.

Is this a user or compiler or C++ standard issue? I'm
using MS VC++ 7.1.3088.

Thanks in advance,
Michael Stembera
 
C

Carl Daniel [VC++ MVP]

Michael said:
Is this a user or compiler or C++ standard issue? I'm
using MS VC++ 7.1.3088.

C++ standard issue - the standard prohibits this kind of specialization.
However, if I define the specialization inside
it compiles fine.

According to Comeau, it shouldn't compile inside the class either.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top