Out-of-class definition of a non-template member of an explicitly specialized class template?


B

BigMan

Why cannot I define a member of an explicitly specialized class
template out of the class template specialization:

template< int i >
struct a
{
static void Do( );
};

template< >
struct a< 2 >
{
static void Do( ); // in-class definition would be OK...
};

// .. however, neither VC 7.1, nor g++ 3.4.2,
// accept out-of-class definition:
template< >
void a< 2 >::Do( )
{
}
 
Ad

Advertisements

C

CrayzeeWulf

BigMan said:
// .. however, neither VC 7.1, nor g++ 3.4.2,
// accept out-of-class definition:
template< >
void a< 2 >::Do( )
{
}
Because, "a<2>::Do()" is not a template. Try:

void a<2>::Do()
{

}

(i.e. without "template<>" at the top).

Thanks,
 
A

Andrey Tarasevich

BigMan said:
Why cannot I define a member of an explicitly specialized class
template out of the class template specialization:

template< int i >
struct a
{
static void Do( );
};

template< >
struct a< 2 >
{
static void Do( ); // in-class definition would be OK...
};

// .. however, neither VC 7.1, nor g++ 3.4.2,
// accept out-of-class definition:
template< >
void a< 2 >::Do( )
{
}
...

'template <>' is used to prefix explicit specializations. If you wanted
to explicitly specialize 'a<i>::Do()' for 'i = 2' without explicitly
specializing the entire class 'a', you could do it as follows

template<int i> struct a {
static void Do( );
};

template<> void a<2>::Do() {
/* ... */
}

But it your code you decided to perform explicit specialization of the
entire class 'a'

template<> struct a<2> {
static void Do( );
};

In this case the out-of-class definition of 'a<2>::Do' has to use the
"regular" syntax

void a<2>::Do() {
/* ... */
}

It cannot be prefixed with 'template<>' in this case.
 
Ad

Advertisements


Top