简体   繁体   English

具有可变参数类型的模板多个可变参数继承

[英]template multiple variadic inheritance with variadic argument types

I need to inherit multiple times the following class, taking variadic arguments as template parameters. 我需要多次继承以下类,以可变参数作为模板参数。

template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
//private
public:

    void RegisterSlot(SignalAddress pSignalFunc, ISlotInvoker<ArgTypes...>* pSlotInvoker)
    {
        //implementation
    }
};

So far I can expand a parameter pack and get multiple class specializations, but with functions taking only one argument. 到目前为止,我可以扩展参数包并获得多个类的专业化知识,但是函数仅采用一个参数。

template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalStorage : public ISignalMap<SignalDispatcherClass, ArgTypes>...
{
};

///////
ISignalStorage<SignalA, int, double, bool> iss; 

For now this allows me to register slot functions with a single argument (int, double or bool - accordingly). 现在,这使我可以使用单个参数(int,double或bool-相应地)注册插槽函数。 What I need is something that would look like: 我需要的是看起来像这样的东西:

ISignalStorage<SignalA, <int, double, bool>, <int, int>, <const char*>> iss;

So far I've been looking into other questions and one appears to be somewhat close to the topic, though I failed to implement or understand it. 到目前为止,我一直在研究其他问题,尽管我没有实现或理解它,但看起来似乎有点接近该主题。 Wish there were a simplier way ( Variadic variadic template templates ) 希望有一个简单的方法( 可变参数模板模板

added: code example 添加:代码示例

struct IDummySlot
{
    void FuncDbl(double)
    {}
    void FuncInt(int)
    {}
    void FuncIntDbl(int, double)
    {}
};

template <class ... Args>
struct ISlotInvoker
{};

template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
public:

    void RegisterSlot(void(IDummySlot::*pSignalFunc)(ArgTypes...), ISlotInvoker<ArgTypes...>* pSlotInvoker)
    {
        return;
    }
};

template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalStorage : public ISignalMap<SignalDispatcherClass, ArgTypes>...
{
};


int main()
{
    ISignalStorage<IDummySlot, int, double> iss;

    ISlotInvoker<int> slot_int;
    ISlotInvoker<double> slot_double;
    ISlotInvoker<int, double> slot_intDouble;

    //iss.RegisterSlot(&IDummySlot::FuncInt, &slot_int); //ambigous
    /*Appears to be that I didn't test it, I just saw that inheritance worked as I expected, but didn't try to invoke*/

    return 0;
}

在此处输入图片说明

The fundamental problem here is that there is no syntax for "squeezing" multiple variadic parameter packs out of a single parameter pack. 这里的根本问题是没有语法可以从单个参数包中“挤压”多个可变参数包。

The usual approach in these kinds of situations is to use a std::tuple to wrap each individual parameter pack, and make a parameter pack out of those tuples: 在这种情况下,通常的方法是使用std::tuple包装每个单独的参数包,并从这些元组中制作一个参数包:

    ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;

Then, it becomes a simple matter of unwrapping each parameter pack from the std::tuple using a specialization: 然后,使用特殊化从std::tuple解开每个参数包就变得很简单:

#include <tuple>


template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
};

// Take a class, and a tuple. Give me an ISignalMap for the class, and
// what's in the tuple.

template<typename cl, typename tuple_t> struct tuple_expansion;

template<typename cl, typename ...tuple_types>
struct tuple_expansion<cl, std::tuple<tuple_types...>> {

    typedef ISignalMap<cl, tuple_types...> type;
};

// Syntactic sugar.    
template<typename cl, typename tuple_t>
using tuple_expansion_t=typename tuple_expansion<cl, tuple_t>::type;

// And a variadic parameter pack of tuples...

template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalStorage : public tuple_expansion_t<SignalDispatcherClass,
                        ArgTypes>...
{
};

class foo;

void bar()
{

    // Note the syntax: pass each "inner" parameter pack wrapped into a
    // tuple.
    ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;

    ISignalMap<foo, int, double> &b=a;
    ISignalMap<foo, double, int> &c=a;
}

For now this allows me to register slot functions with a single argument (int, double or bool - accordingly). 现在,这使我可以使用单个参数(int,double或bool-相应地)注册插槽函数。 What I need is something that would look like: 我需要的是看起来像这样的东西:

ISignalStorage<SignalA, <int, double, bool>, <int, int>, <const char*>> iss;

As better explained by Sam Varshavchik, this type of needs is usually managed wrapping packs of types as template arguments for another class. 正如Sam Varshavchik更好地解释的那样,这种类型的需求通常通过包装类型的包装来作为另一类的模板参数来管理。 Usually is used std::tuple , that offer some handy tools to manage pack of types, but you can also define a trivial template class/struct as follows ( TW for "type wrapper") 通常使用std::tuple ,它提供了一些方便的工具来管理类型包,但是您也可以按以下方式定义简单的模板类/结构( TW表示“类型包装器”)

template <typename...>
struct TW
 { };

I propose a slightly different solution that is wrapper neutral (so you can use with classic std::tuple , with a custom TW or also mixing they) 我提出了一个与包装无关的稍微不同的解决方案(因此,您可以将其与经典std::tuple一起使用,还可以使用自定义TW或将它们混合使用)

template <typename, typename>
struct ISignalStorageHelper;

template <typename T, template <typename...> class C, typename ... Ts>
struct ISignalStorageHelper<T, C<Ts...>> : public ISignalMap<T, Ts...>
 { };

template <typename SignalDispatcherClass, typename ... Tuples>
class ISignalStorage
   : public ISignalStorageHelper<SignalDispatcherClass, Tuples>...
 { };

This way we can avoid the using to extract the type inside the intermediate struct but ISignalStorage inherit also from ISignalStorageHelper structs (I hope isn't a problem). 这样,我们可以避免using提取中间结构体中的type ,但是ISignalStorage也继承自ISignalStorageHelper结构体(我希望这不是问题)。

You can define ISignalStorage objects using tuples 您可以使用元组定义ISignalStorage对象

ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;

custom pack wrapper as TW 自定义包装包装为TW

ISignalStorage<foo, TW<int, double>, TW<double, int>> a;

or also mixing 或混合

ISignalStorage<foo, std::tuple<int, double>, TW<double, int>> a;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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