7
votes

It is continuation of this question. I am specifically interested if the partial specialization of a member class like this:

struct FooParent {
    template <class>
    struct Bar{ };
};

struct Foo: FooParent {
    template <class T>
    struct Bar<T*> {};
};

I know this can be done inside a namespace scope:

template <class T>
struct Foo::Bar<T*>{ };

But I'm also specifically interested in in-class partial specialization at the level of derived class.

Both clang and gcc complains when encounter a former:

clang states that there is an explicit template specialization which obviously does not occur:

error: explicit specialization of 'Bar' in class scope

gcc is a little bit less verbose here and says that the specialization of the member template must be performed at a namespace scope which obviously is not a case for not derived class.

error: specialization of 'template struct FooParent::Bar' must appear at namespace scope

Is gcc right here in his error message?

1
I guess this rules on it. You cannot define primary template in the derived class, so you cannot declare a partial specialization as well. - A class template partial specialization may be declared or redeclared in any namespace scope in which the corresponding primary template may be definedskypjack
@skypjack not the case as it is about allowing the undesired namespace scope specialization not disallowing anything - see the reference question and discussion in comments...W.F.
I got it as this is allowed, thus everything else is disallowed. You are saying instead that it is allowed, but there could be something else declared as allowed somewhere in the standard, right? So you want an explicit rule that disallows it. Did I get your goal rightly?skypjack
@skypjack Not exactly, please see this. This suggests that it is perfectly legal to in-class partial specialization of the struct. The comment of Johannes Schaub - litb on my previous question goes even a step beyond - it suggests that the partial specialization is a template and as such can be declared wherever the template can be declared... the scope of derived class IMHO should allow to declare template, no? Am I missing something?W.F.
I'm not a native speaker, you know. So I could be wrong as well. Anyway it looks pretty clear to me. That's also why I cannot see what's wrong in it from your point of view. Let me know tomorrow if your doubts are still there. :-) ... That said, good question: +1 - I've never tried to do such a thing before actually and never thought if it was possible or not.skypjack

1 Answers

1
votes

I'm trying to sum up what I said in the comments to the question, as requested by the OP.


I guess [temp.class.spec]/5 is enough to reply to the question.
In particular:

A class template partial specialization may be declared or redeclared in any namespace scope in which the corresponding primary template may be defined [...].

In this case, what actually rule on it is where the primary template can be defined.
In the example, you are trying to declare (and contextually define, but it's first of all a declaration) a partial specialization in a derived class.

The short answer is: you cannot define the primary template in the derived class, so you cannot declare a partial specialization in that class as well.

If it was possible , the following would have been possible too:

struct FooParent {
    template <class>
    struct Bar;
};

struct Foo: FooParent {
    template <class T>
    struct FooParent::Bar<T*> {};
};

Or this one if you prefer:

struct Foo: FooParent {
    template <class T>
    struct Bar<T*> {};
};

Unfortunately (?) they are not allowed and this would suffice to tell you that your attempt to specialize the class template is invalid as well.

Anyway, let's consider it a bit further.
The primary template is part of the member specification of Foo (see here for further details).
Thus, the question - where can I define such a template?, quickly becomes - where can I define any other member of the class?.
Again, the answer is - not in the scope of a derived class.


I'm pretty sure that a language-lawyer would give you more direct and straightforward explanation.
I'm also pretty sure that the same language-lawyer would curse me for having mentioned the wrong sections of the standard.
Anyway, I hope the few examples above can give you a base point from which to start.