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( )
{
}
 
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top