多级递归模板

时间:2018-01-05 10:41:15

标签: c++

我想实现一个递归模板:

template<unsigned int M,unsigned int N>
class Special:Special<M,N-1>
{
    void * get()
     {
           return (Special <M,N-1>*)this;
     }
}

template <>
class Special<0,0>
 { 
       void * get()
        {
               return this;
        }
 }

 void main()
 {
       Special<3,2> specialobj;
  }

如何获得包含所有基础对象(3 * 2)的specialobj,并且每个基础对象都有一个get函数?

1 个答案:

答案 0 :(得分:0)

您需要部分专门化基础案例模板。

template<unsigned int M, unsigned int N>
class Special :Special<M, N - 1>
{
    void * get() { return (Special <M, N - 1>*)this; }
};

template <unsigned int M>
class Special<M, 0>
{
    void * get() { return this; }
};

void main()
{
    Special<3, 2> specialobj;
}

根据以下评论的新要求,代码如下所示:

template<unsigned int M, unsigned int N>
class Special : public Special<M, N - 1>
{
    auto get() { return this; }
};

template <unsigned int M>
class Special<M, 0> : public Special <M - 1, 2>
{
    void * get() { return this; }
};

template <>
class Special<0, 0>
{
    void * get() { return this; }
};

有了另一个更新,它将如下所示:

template<unsigned int NStart, unsigned int M, unsigned int N>
class SpecialInternal : public SpecialInternal<NStart, M, N - 1>
{
    auto get() { return this; }
};

template <unsigned int NStart, unsigned int M>
class SpecialInternal<NStart, M, 0> : public SpecialInternal <NStart, M - 1, NStart>
{
    auto get() { return this; }
};

template <unsigned int NStart>
class SpecialInternal<NStart, 0, 0>
{
    auto get() { return this; }
};

template<unsigned int M, unsigned int N>
class Special : public SpecialInternal<N, M, N>
{
    auto get() { return this; }
};

最后测试:

void main()
{
    Special<3, 2> specialobj;

    cout << std::boolalpha;
    cout << "<3,2>: " << is_base_of<SpecialInternal<2, 3, 2>, Special<3, 2>>::value << endl;
    cout << "<3,1>: " << is_base_of<SpecialInternal<2, 3, 1>, Special<3, 2>>::value << endl;
    cout << "<3,0>: " << is_base_of<SpecialInternal<2, 3, 0>, Special<3, 2>>::value << endl;
    cout << "<2,2>: " << is_base_of<SpecialInternal<2, 2, 2>, Special<3, 2>>::value << endl;
    cout << "<2,1>: " << is_base_of<SpecialInternal<2, 2, 1>, Special<3, 2>>::value << endl;
    cout << "<2,0>: " << is_base_of<SpecialInternal<2, 2, 0>, Special<3, 2>>::value << endl;
    cout << "<1,2>: " << is_base_of<SpecialInternal<2, 1, 2>, Special<3, 2>>::value << endl;
    cout << "<1,1>: " << is_base_of<SpecialInternal<2, 1, 1>, Special<3, 2>>::value << endl;
    cout << "<1,0>: " << is_base_of<SpecialInternal<2, 1, 0>, Special<3, 2>>::value << endl;
    cout << "<0,2>: " << is_base_of<SpecialInternal<2, 0, 2>, Special<3, 2>>::value << endl;
    cout << "<0,1>: " << is_base_of<SpecialInternal<2, 0, 1>, Special<3, 2>>::value << endl;
    cout << "<0,0>: " << is_base_of<SpecialInternal<2, 0, 0>, Special<3, 2>>::value << endl;
}

输出:

  

&lt; 3,2&gt;:真实的   &lt; 3,1&gt;:真实的   &lt; 3,0&gt ;:真实   &lt; 2,2&gt;:真实的   &lt; 2,1&gt;:真实的   &lt; 2,0&gt;:真实的   &lt; 1,2&gt;:真实的   &lt; 1,1&gt;:真实的   &lt; 1,0&gt;:真实的   &lt; 0,2&gt ;:真实   &lt; 0,1&gt;:真实的   &lt; 0,0&gt;:true