inner template class specialization

C

Christof Warlich

Hi,

I need to specialize an inner template class, but gcc
refuses to compile. Here is a minimal example exposing the issue:

template<int a> struct Outer {
template<int b> struct Inner {
};
};
template<int a> template<> struct Outer<a>::Inner<0> {
};

gcc reports:

$ g++ -c inner.cc
inner.cc:5: error: invalid explicit specialization before ?>? token
inner.cc:5: error: enclosing class templates are not explicitly specialized
inner.cc:5: error: template parameters not used in partial specialization:
inner.cc:5: error: 'a'

Is this a gcc-specific bug or am I doing something wrong?

Thanks,

Christof
 
M

Markus Moll

Hi

Christof said:
$ g++ -c inner.cc
inner.cc:5: error: invalid explicit specialization before ?>? token
inner.cc:5: error: enclosing class templates are not explicitly
specialized inner.cc:5: error: template parameters not used in partial
specialization:
inner.cc:5: error: 'a'

Is this a gcc-specific bug or am I doing something wrong?

It's not a bug. You are not allowed to explicitly specialize the inner
template that way ("the declaration shall not explicitly specialize a class
member template if its enclosing class templates are not explicitly
specialized as well", 14.7.3/18)

Markus
 
C

Christof Warlich

Markus said:
It's not a bug. You are not allowed to explicitly specialize the inner
template that way ("the declaration shall not explicitly specialize a class
member template if its enclosing class templates are not explicitly
specialized as well", 14.7.3/18)

Does this mean that specialization of an inner class does not work at all?
I tried several things, but without success.

Nevertheless, your wording:
> You are not allowed to explicitly specialize the inner template "that way"

gives me some hope that it _is_ possible. Any ideas how to do it right?

Thanks for any help,

Christof
 
M

Michael DOUBEZ

Christof Warlich a écrit :
gives me some hope that it _is_ possible. Any ideas how to do it right?


You can use a helper somewhere:

template<int a,int b>
struct helper
{
//general case
};

template<int a>
struct helper<a,0>
{
//specific case
};


template<int a> struct Outer {
template<int b> struct Inner {
typedef helper<a,b> type;
};
};
 
C

Christof Warlich

Christof said:
Nevertheless, your wording:

way"

gives me some hope that it _is_ possible. Any ideas how to do it right?

Just found the solution myself:

template<int a> struct Outer {
template<int b, int dummy> struct Inner {
};
};
template<int a>
template<int dummy>
struct Outer<a>::Inner<0, dummy> {
};

Again, thanks for your help, it just gave the hint that I was in need for :).
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top