嵌套的模板类

Nested Template Classes

本文关键字:嵌套      更新时间:2023-10-16

我希望能够创建一个通用的嵌套模板,这样我就可以找到所有类的总大小。首先,想象一下类A、B、C等,每个类都有一个mSize成员和GetSize()函数。我做以下过程:

int main()
{
    using Abc = A<B<C<>>>;  // Imagine it is defined similarly to this for now.
    Abc abc;
    std::cout << abc.GetSize() << std::endl;
    // For abc.GetSize(), this will do the following:
    // 1. Go into A::GetSize().
    // 2. This will return A::mSize + B::GetSize()
    // 3. This will go into B::GetSize()
    // 4. This will return B::mSize + C::GetSize()
    // 5. Etc
    // Overall, we will have the total size of A+B+C as
    // A::mSize + B::mSize + C::mSize.
    return 0;
}

它将递归地遍历每个模板类,直到结束并调用GetSize()。我目前的尝试是使用模板模板和可变模板。

template <template<typename> class First, template<typename> class ...Args>
class A
{
public:
    int GetSize() const
    {
        First<Args...> foo;
        return mSize + foo.GetSize();
    }
private:
    int mSize{1};
};
template <template<typename> class First, template<typename> class ...Args>
class B
{
public:
    int GetSize() const
    {
        First<Args...> foo;
        return mSize + foo.GetSize();
    }
private:
    int mSize{2};
};
template <template<typename> class First, template<typename> class ...Args>
class C
{
public:
    int GetSize() const
    {
        First<Args...> foo;
        return mSize + foo.GetSize();
    }
private:
    int mSize{3};
};

这显然没有奏效。我真的希望能够实现int main()中描述的过程。

备注

  1. 这些类不一定要包括在内,也不一定要按顺序排列。我们可以有A<C>B<E<C<F<>>>>。理想情况下,它可以无限长。

  2. 我不想使用多态性,希望它在运行时得到解决。我可以让它们都从同一个类继承,创建一个std::vector<Parent*>,对每个子类进行push_back,并使用GetSize()进行迭代。如果能够定义唯一的类型,如A<B<>>A<B<C<>>>等,那就太好了。

由于mSize对所有实例都是相同的,因此您的方法应该是static,并且由于它看起来像是一个常量,因此它应该是一个constexpr

下面是一个使用通用模板,然后用特定大小部分实例化它的实现:

template <int Size, typename T>
struct Holder {
    static constexpr int GetSize() {
        return Size + T::GetSize();
    }
};
template <int Size>
struct Holder<Size, void> {
    static constexpr int GetSize() {
        return Size;
    }
};
template <typename T = void>
using A = Holder<1, T>;
template <typename T = void>
using B = Holder<2, T>;
template <typename T = void>
using C = Holder<3, T>;

然后你可以测试:

using AB = A<B<>>;
using ABC = A<B<C<>>>;
static_assert(AB::GetSize() == 1 + 2, "Oops!");
static_assert(ABC::GetSize() == 1 + 2 + 3, "Oops!");

当然,你可以制作ABC。。。扩展Holder,而不是在需要时部分实例化它。

您可以执行以下操作:

#include <iostream>
#include <type_traits>
using namespace std;
template <class T>
struct A {
  static constexpr int size = 1;
  using inner_type = T;
};
template <class T>
struct B {
  static constexpr int size = 2;
  using inner_type = T;
};
//template <class T>
struct C {
  static constexpr int size = 3;
  using inner_type = void;
};
template <class T, class = void>
struct TotalSizeGetter {
  static constexpr int get() {
    return T::size + TotalSizeGetter<typename T::inner_type>::get(); 
  }
};
template <class T>
struct TotalSizeGetter<T, typename enable_if<is_void<typename T::inner_type>::value>::type> {
  static constexpr int get() {
    return T::size;
  }
};
int main() {
  cout << TotalSizeGetter<A<B<C>>>::get() << endl;
}

这使用了c++11 constexprenable_if,但我认为这不是一个限制,因为你在问题中使用了术语可变模板。。。