嵌套模板类的特殊化语法 [英] Syntax for specialization of nested template class

查看:106
本文介绍了嵌套模板类的特殊化语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚嵌套模板类的显式特化的正确语法。以下代码将更好地说明:

I'm trying to figure out the correct syntax for explicit specialization of a nested template class. The following code will better illustrate:

struct Column_Major;
struct Row_Major;

template<size_t rows, size_t cols, typename T, typename Allocator>
class Matrix
{

    /* bunch of members */
    template <typename storage = Column_Major>
    class Iterator
    {
        /* bunch of members */
    };
};

我想为 template<类Matrix< ...> :: Iterator< Row_Major ,但是语法是躲避我。我有一个怀疑,是不可能明确地专门化Iterator类,而没有包含类,Matrix的明确专门化。但我会非常高兴,如果有办法做到这一点。

I'd like to write an explicit specialization for template <> class Matrix<...>::Iterator<Row_Major, but the syntax is eluding me. I have a suspicion that it is not possible to explicitly specialize the Iterator class without an explicit specialization of the containing class, Matrix. But I would be very happy if there is a way to do this.

我知道我可以使Iterator类是一个单独的类,而不是Matrix类的成员,但具有类嵌套的类,这允许我完全访问Matrix类的模板参数和数据类,这简化了事情。我知道如果我需要,我可以解决这个问题,但我会先调查并了解嵌套方法的可能性。

I know I could make the Iterator class a separate class, not a member of the Matrix class, but having the classes nested as such allows me full access to the template parameters and datamebers of the Matrix class, which simplifies things. I know I could work around this if I need to, but I'd first like to investigate and understand the possibilities for the nested approach.

谢谢,
Shmuel

Thanks, Shmuel

推荐答案

对于显式专门化,你需要在inner之前专门化外部类,你可以查看此问题例如

For explicit specialization, you need to specialize the outer class before the inner, you can see this question for example.

有一个解决方法是使用部分专门化:

There is a workaround that is using partial specialization:

template<size_t rows, size_t cols, typename T, typename Allocator>
class Matrix
{

    //                           Notice the additionnal dummy parameter
    //                                       vvvvvvvvvvvvv
    template <typename storage = Column_Major, bool = true>
    class Iterator
    {
    };

    // Specialization
    template <bool dummy>
    class Iterator<Row_Major, dummy>
    {
    };
};

这篇关于嵌套模板类的特殊化语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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