简体   繁体   中英

How can I call a function of all variadically inherited classes?

I'd be grateful to any template gurus out there for some help on this one. I'm using CRTP for a mixin class and would like to be able to pass arguments to a function of the derived class and have it call the inherited functions of all mixins and forward the appropriate number of arguments. Eg:

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)),...);
    }
}

And use like:

entity.Initialize(42,42.0,42.0);

Is it possible to pass the appropriate number of arguments to each? The code above of course doesn't work but my idea was to try the method of instantiating an empty template with the types of each of the arguments (ie Variad<int, double> v1; Variad<> v2; Variad<double> v3; ) and pass a variad containing all those into the function along with the arguments but I can't seem to figure out how to split the arguments correctly.

Is it possible to pass the appropriate number of arguments to each?

Not in a simple way, as far I can understand.

Surely it's possible, counting argument of every method and using recursion (also recursive variadic lambdas) and SFINAE.

I've developed the following example just because I love template metaprogramming; but I'm the first to say that it's a delirious nightmare.

#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);
 }

As said in a comment, this solution doesn't works in case of overloading, or template, for Initialize() in the single Components .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM