如何调用所有可变继承类的函数?

时间:2019-06-20 21:18:01

标签: c++ variadic-templates multiple-inheritance variadic-functions fold

对于任何模板专家,我都将不胜感激,希望对此有所帮助。我正在将CRTP用于mixin类,并希望能够将参数传递给派生类的函数,并使其调用所有mixin的继承函数并转发适当数量的参数。例如:

template<Host> struct Mixin1 { void Initialize(int, double); };
template<Host> struct Mixin2 { void Initialize(); };
template<Host> struct Mixin3 { void Initialize(double); };

template <template<class> class... Components>
struct Entity : public Components<Entity<Components...>>...
{
    template<template<class> class ...Types, template<template<typename>typename...> class T, class... Args>
    void Initialize(const T<Types...>&, Args&&... args) {
      (Types<Entity<Types>>::Initialize(forward<Types>(args)),...);
    }
}

并像这样使用:

entity.Initialize(42,42.0,42.0);

是否可以将适当数量的参数传递给每个参数?上面的代码当然不起作用,但是我的想法是尝试使用每个参数的类型(即Variad<int, double> v1; Variad<> v2; Variad<double> v3;)实例化一个空模板的方法,并将包含所有这些参数的variad以及参数,但我似乎无法弄清楚如何正确分割参数。

1 个答案:

答案 0 :(得分:1)

  

是否可以将适当数量的参数传递给每个参数?

据我了解,这不是简单的方法。

当然有可能,计算每种方法的参数并使用递归(也包括递归可变参数lambda)和SFINAE。

仅因为我喜欢模板元编程,所以开发了以下示例;但我是第一个说这是一场疯狂的噩梦。

#include <utility>
#include <iostream>
#include <type_traits>

template <typename R, typename T, typename ... As>
constexpr std::size_t numArgs (R(T::*)(As...))
 { return sizeof...(As); }

template <typename>
struct Mixin1
 {
   void Initialize (int i, double d)
    { std::cout << "I1: " << i << ", " << d << std::endl; }
 };

template <typename>
struct Mixin2
 {
   void Initialize ()
    { std::cout << "I2:" << std::endl; }
 };

template <typename>
struct Mixin3
 {
   void Initialize (double d)
    { std::cout << "I3: " << d << std::endl; }
 };

template <template <typename> class ... Cs>
struct Entity : public Cs<Entity<Cs...>>...
 {
   template <std::size_t Pos, typename ... Ts,
             typename F, std::size_t I0, std::size_t ... Is,
             typename ... As>
   std::enable_if_t<(Pos == I0)>
      Ih2 (F const & f, std::index_sequence<I0, Is...> const &,
           As && ... as)
    { 
      f(); // exec an Initialize();

      Ih1<Ts...>(std::index_sequence<Is...>{}, std::forward<As>(as)...);
    }

   template <std::size_t Pos, typename ... Ts,
             typename F, std::size_t I0, std::size_t ... Is,
             typename A0, typename ... As>
   std::enable_if_t<(Pos < I0)>
      Ih2 (F const & f, std::index_sequence<I0, Is...> const & is,
           A0 && a0, As && ... as)
    { Ih2<Pos+1u, Ts...>
       ([&a0, &f](auto && ... as2) { f(std::forward<A0>(a0),
                                     std::forward<decltype(as2)>(as2)...); },
        is, std::forward<As>(as)...); }

   template <int = 0>
   void Ih1 (std::index_sequence<> const &)
    { }

   template <typename T0, typename ... Ts,
             std::size_t ... Is, typename ... As>
   void Ih1 (std::index_sequence<Is...> const & is, As && ... as)
    { Ih2<0u, Ts...>
       ([this](auto && ... as2)
           { T0::Initialize(std::forward<decltype(as2)>(as2)...); },
        is, std::forward<As>(as)...); }

   template <typename ... As>
   void Initialize (As && ... args)
    { Ih1<Cs<Entity<Cs...>>...>
       (std::index_sequence<numArgs(&Cs<Entity<Cs...>>::Initialize)...>{},
        std::forward<As>(args)...); }
 };

int main ()
 {
   Entity<Mixin1, Mixin2, Mixin3> entity;

   entity.Initialize(1, 2.0, 3.0);
 }

如评论中所述,对于单个Initialize()中的Components,此解决方案在过载或模板的情况下无效。