在派生类中执行成员模板类的部分类内专业化是否合法 [英] Is it legal to perform partial in-class specialization of a member template class in derived class

查看:84
本文介绍了在派生类中执行成员模板类的部分类内专业化是否合法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是这个问题的延续.我特别感兴趣成员类部分专业化:

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*>{ };

但是我对派生类的 in-class 部分专业化特别感兴趣.

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

clang和gcc在遇到前者时都抱怨:

Both clang and gcc complains when encounter a former:

clang指出有一个明确的模板专门化,显然不会发生:

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

错误:"Bar"在类范围内的显式专业化

error: explicit specialization of 'Bar' in class scope

gcc在这里稍稍冗长一些,并说成员模板的特殊化必须在命名空间范围内执行,这显然不是未派生类的情况.

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.

错误:模板结构FooParent :: Bar"的特殊化必须出现在命名空间范围内

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

gcc是否在他的错误消息中?

Is gcc right here in his error message?

推荐答案

我试图按照OP的要求总结我在问题注释中所说的话.

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

我猜 [temp.class.spec]/5 足以回答这个问题.
特别是:

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.

无论如何,让我们再考虑一下.
主要模板是Foo成员规范的一部分(请参见此处有关更多详细信息).
因此,问题-在哪里可以定义这样的模板?,很快就会变成-在哪里可以定义该类的其他成员?.
同样,答案是-不在派生类的范围内.

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.

这篇关于在派生类中执行成员模板类的部分类内专业化是否合法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆