专门化成员模板而不专门化其父级 [英] specialize a member template without specializing its parent

查看:157
本文介绍了专门化成员模板而不专门化其父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类模板嵌套在另一个模板中。部分专业化很容易:我只是声明另一个模板< ...>

I have a class template nested inside another template. Partially specializing it is easy: I just declare another template< … > block inside its parent.

但是,我需要另一个部分专门化来指定所有的本地模板参数。这使它成为一个明确的专业化。显式特化,无论什么原因,必须在命名空间范围。要在父类之外声明它,必须提名父类,这需要一个非空的模板参数列表。这意味着部分专业化。部分专业化是我正在做的,它应该在任意外部范围工作。但是GCC和Comeau都没有在部分专业化形式参数中识别父提名中的模板参数。

However, I need another partial specialization that happens to specify all its local template arguments. This makes it into an explicit specialization. Explicit specializations, for whatever reason, must be at namespace scope. To declare it outside its parent class, the parent must be nominated, which requires a non-empty template argument list. This implies partial specialization. Partial specialization is what I'm doing, and it's supposed to work at arbitrary outer scope. But both GCC and Comeau fail to identify the template parameter in the parent nomination with the partial specialization formal arguments.

template< class X > struct A {
    template< class Y > struct B; // initial declaration OK

    template< class Z >
    struct B< A< Z > > {}; // partial OK as long as there's a local arg

    template<> // ERROR: this syntax triggers explicit specialization
    struct B< int > {};
};

template<> // ERROR: can't nest template<>s here (why?)
template< class X > // ERROR: can't deduce X from type of A<X>::B<int> (why?)
struct A< X >::B< int > {};

(我留下了所有的非工作代码;适当地注释以试图有意义。

(I left all my non-working code in; comment it appropriately to attempt to make sense.)

推荐答案

在C ++标准14.7.3 / 18下是非法的:

It is illegal under C++ standard 14.7.3/18:

....如果类成员
模板的封闭类模板没有被明确指定为专门的
,那么声明不会明确地专门化。

.... the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.

这篇关于专门化成员模板而不专门化其父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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