By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,273 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

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

P: n/a
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( )
{
}

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
BigMan wrote:
// .. 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,
--
CrayzeeWulf
Jul 23 '05 #2

P: n/a
BigMan wrote:

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.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #3

P: n/a
Thanks a lot to both of you!

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.