Defining templated member function outside templated class

C

chhenning

Hi there, I'm surprised the following construct seems to be illegal:

template< typename T >
struct A
{
template< typename K >
void do_some( const K& );
};

template< typename T >
template< typename K >
void A<T>::do_some<K>( const K& k )
{}

I have tried several compilers, include Comeau and they all fail to
compile. Is the above code not allowed by the current standard?

Regards,
Christian
 
I

Ian Collins

chhenning said:
Hi there, I'm surprised the following construct seems to be illegal:

template< typename T >
struct A
{
template< typename K >
void do_some( const K& );
};

template< typename T >
template< typename K >
void A<T>::do_some<K>( const K& k )
{}
template< typename T >
template< typename K >
void A<T>::do_some( const K& k )
{}
 
B

Bo Persson

chhenning said:
Hi there, I'm surprised the following construct seems to be illegal:

template< typename T >
struct A
{
template< typename K >
void do_some( const K& );
};

template< typename T >
template< typename K >
void A<T>::do_some<K>( const K& k )
{}

I have tried several compilers, include Comeau and they all fail to
compile. Is the above code not allowed by the current standard?

No, but only becase it is wrong. :)

The code do_some<K> would indicate a specialization of the function,
which is not allowed. You should instead try:

template< typename T >
template< typename K >
void A<T>::do_some( const K& k )
{}



Bo Persson
 
E

Erik Wikström

Hi there, I'm surprised the following construct seems to be illegal:

template< typename T >
struct A
{
template< typename K >
void do_some( const K& );
};

template< typename T >
template< typename K >
void A<T>::do_some<K>( const K& k )
{}

I have tried several compilers, include Comeau and they all fail to
compile. Is the above code not allowed by the current standard?

I do not know why this is illegal (neither the motivation nor the
chapter and verse of the standard) but the following works (in VC++ 2008
Express):

template< typename T >
template< typename K >
void A<T>::do_some( const K& k )
{}
 
I

Ian Collins

Erik said:
I do not know why this is illegal (neither the motivation nor the
chapter and verse of the standard) but the following works (in VC++ 2008
Express):
It appears as a partial specialisation.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top