具有可变参数类型的模板多个可变参数继承 [英] template multiple variadic inheritance with variadic argument types

查看:135
本文介绍了具有可变参数类型的模板多个可变参数继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要多次继承以下类,以可变参数作为模板参数.

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; 

现在,这使我可以使用单个参数(int,double或bool-相应地)注册插槽函数. 我需要的是看起来像这样的东西:

For now this allows me to register slot functions with a single argument (int, double or bool - accordingly). 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)

已添加: 代码示例

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.

在这种情况下,通常的方法是使用std::tuple包装每个单独的参数包,并从这些元组中制作一个参数包:

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:

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

然后,使用专门化方法从std::tuple解开每个参数包就变成了简单的事情:

Then, it becomes a simple matter of unwrapping each parameter pack from the std::tuple using a specialization:

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

这篇关于具有可变参数类型的模板多个可变参数继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆