Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a function with types of arguments taken from a foreign parameter pack

Suppose I have some kind of type list

template<typename... Types> struct TypeList {};

Now in some other class I can generate such a TypeList in a variety of ways.

template<class T> struct MyClass {
    using MyList = TypeList<T, typename Something<T>::type, SomethingElse>;
    // ...
};

How can I declare a method with types of arguments extracted from this type list? For example, if I set MyList = TypeList<int, float, const char*>, I wish a method

void my_method(int, float, const char*)

to be declared.

like image 636
Tigran Saluev Avatar asked Jun 07 '16 21:06

Tigran Saluev


1 Answers

You could derive from a base class that implements the method:

template <typename> struct MethodProvider;

template <typename ...Args>
struct MethodProvider<TypeList<Args...>>
{
    void my_method(Args ...args);
};

template <typename T>
struct MyClassAux
{
    using MyList = TypeList<T, typename Something<T>::type, SomethingElse>;
};

template <typename T>
struct MyClass
    : private MyClassAux<T>
    , private MethodProvider<typename MyClassAux<T>::MyList>
{
    using typename MyClassAux<T>::MyList;
    using MethodProvider<typename MyClassAux<T>::MyList>::my_method;

    // ...
};
like image 65
Kerrek SB Avatar answered Nov 20 '22 08:11

Kerrek SB