Syntax for specialization of nested template class

让人想犯罪 __ 提交于 2019-11-28 10:01:07
Synxis

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

You can make Synxis answer (use of defaulted dummy parameter) even more clean with C++11:

/// template <typename X>, not needed for the example
struct Outer
{
private:
    template <typename A, typename D = void>
    struct Inner
    {
        Inner()  { cout << "default" << endl; }
    };
    template <typename D>
    struct Inner<int,D>
    {
        Inner()  { cout << "int" << endl; }
    };  
public:
    template <typename T>
    using  Nested = Inner<T>;
};

The advantage of this improvement is that the signature of Nested has only one template parameter, which I think will help if you want to match it correctly in template meta-programming.

I'm surprised the template parameter for the nested class isn't a parameter of the parent class instead.

The nested class can use the template parameters of the parent and this more closely ties the nested class to the parent. Your use of the word iterator suggests this is good, the iterator surely iterating over the same type the parent contains?

I'd do it like this:

template <class T>
class Outer
{
public:
    class Inner
    {
        void Fn( T in )
        {
        }
    };
};

// specialisation
void Outer<double>::Inner::Fn( double in )
{

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!