Class Template Member Class Template Specialisaton Query

S

Simon G Best

Hello!

I have a query regarding explicit specialisation of class templates
which are themselves members of class templates.

Here's what I want to do:

template< class T > struct pink {

template< class U > struct floyd;

};

template< class T > template< class U >
struct pink<T>::floyd {

pink seamus();

U eugene();

};

/* Here's the specialisation I'm wondering
about (with line numbers given in
comments on the left): */

template< class T >
/*19*/ struct pink<T>::floyd<int> {

/*21*/ pink seamus();

float eugene();

};

Comeau's online compiler does fine with that :) but GCC 3.2.3 does not
:-( Instead, g++ says:

[source file]:19: template parameters not used in partial
specialization:
[source file]:19: `T'
[source file]:21: syntax error before `(' token

However, I can get it to (sort of) work if I change the specialisation
definition to:

template< class T >
struct pink<T>::floyd<int> {

pink<T> seamus();

float eugene();

};

though it still warns about T being an unused template parameter.

I'd just like to confirm my suspicion that GCC 3.2.3 is incorrect on
this, and that the original version of my code is correct :)

Thank you!

Simon
 
G

Gianni Mariani

Simon said:
Hello!

I have a query regarding explicit specialisation of class templates
which are themselves members of class templates.

Here's what I want to do:

template< class T > struct pink {

template< class U > struct floyd;

};

template< class T > template< class U >
struct pink<T>::floyd {

pink seamus();

U eugene();

};

/* Here's the specialisation I'm wondering
about (with line numbers given in
comments on the left): */

template< class T >
/*19*/ struct pink<T>::floyd<int> {

/*21*/ pink seamus();

float eugene();

};


You could theoretically do it with 2 non nested structs.

template<class T, class U> struct floyd;

template<class T> struct pink;

template<class T, class U>
struct floyd {

pink<T> seamus();
U eugene();
};

template<class T>
struct floyd<T,int> {

pink<T> seamus();
float eugene();
};

Comeau's online compiler does fine with that :) but GCC 3.2.3 does not
:-( Instead, g++ says:

[source file]:19: template parameters not used in partial
specialization:
[source file]:19: `T'
[source file]:21: syntax error before `(' token

However, I can get it to (sort of) work if I change the specialisation
definition to:

template< class T >
struct pink<T>::floyd<int> {

pink<T> seamus();

float eugene();

};

though it still warns about T being an unused template parameter.

I suspect this is a problem with g++. File a bug with gcc, they're
usually very responsive.
I'd just like to confirm my suspicion that GCC 3.2.3 is incorrect on
this, and that the original version of my code is correct :)

I tried your original version on gcc 3.3.1, you could aloso try a 3.4
devel version of gcc, they're trying to fix things like this.
 
S

Simon G Best

Gianni said:
You could theoretically do it with 2 non nested structs.

Yeah, it looks like that's what I'll have to do (though it looks untidy,
as floyd really belongs to pink) :-(

(Or I could do interesting things behind the scenes with templates
outside the containing template, and then define the member template in
terms of those exterior templates. But I don't like 'clever' solutions
like that - I prefer to work /with/ the language, rather than try to
overcome it.)

[Example of two nonnested templates snipped.]
I suspect this is a problem with g++. File a bug with gcc, they're
usually very responsive.

Well, I've since learned (from johnchx in comp.lang.c++.moderated) that
the standard actually forbids what I was trying to do (14.7.3.18 says
that it isn't allowed). To explicitly specialise a member class
template, the enclosing class templates also need to be specialised :-(

GCC 3.2.3 is actually correctly reporting the error when I try to do it
the obvious way:

template< class T > template<>
struct pink<T>::floyd<int> { /* etc... */ };

It reports the lack of specialisation of the enclosing class, which is
consistent with the standard after all (and so my faith in GCC is
restored :) ).
I tried your original version on gcc 3.3.1, you could aloso try a 3.4
devel version of gcc, they're trying to fix things like this.

Thanks :) but it seems I'm going to need to redesign things instead :-(

Simon
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top