嵌套模板 类

Nested Template Classes

我希望能够创建一个通用的嵌套模板,这样我就可以找到所有 classes 的总大小。首先,假设 classes 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;
}

它将递归遍历每个模板class直到结束并调用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. 这些 class 不一定必须包括在内,也不一定要按顺序排列。我们可以 A<C>B<E<C<F<>>>>。理想情况下,它可以无限长。

  2. 不想用多态,希望在运行时解析。我可以让它们都继承自同一个 class,创建一个 std::vector<Parent*>,每个子 class push_back,然后使用 GetSize() 进行迭代。如果能够定义独特的类型,如A<B<>>A<B<C<>>>

  3. ,那就太好了

你可以这样做:

#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 但我认为这不是限制,因为您在问题中使用术语可变参数模板...

由于您的 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!");

当然你可以 A, B, C, ... extends Holder 而不是部分实例化,如果你需要的话。