具有可变参数类型的模板多重可变继承
template multiple variadic inheritance with variadic argument types
我需要多次继承以下class,将可变参数作为模板参数。
template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
//private
public:
void RegisterSlot(SignalAddress pSignalFunc, ISlotInvoker<ArgTypes...>* pSlotInvoker)
{
//implementation
}
};
到目前为止,我可以扩展参数包并获得多个 class 特化,但函数只接受一个参数。
template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalStorage : public ISignalMap<SignalDispatcherClass, ArgTypes>...
{
};
///////
ISignalStorage<SignalA, int, double, bool> iss;
目前,这允许我使用单个参数(相应的 int、double 或 bool)注册槽函数。
我需要的是这样的东西:
ISignalStorage<SignalA, <int, double, bool>, <int, int>, <const char*>> iss;
到目前为止,我一直在研究其他问题,其中一个似乎与主题有些接近,但我未能实施或理解它。希望有更简单的方法 (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;
}
这里的根本问题是单个参数包中没有 "squeezing" 多个可变参数包的语法。
在这种情况下,通常的方法是使用 std::tuple
来包装每个单独的参数包,并从这些元组中创建一个参数包:
ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;
然后,使用特化从 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). What I need is something that would look like:
ISignalStorage<SignalA, <int, double, bool>, <int, int>, <const char*>> iss;
正如 Sam Varshavchik 更好地解释的那样,这种类型的需求通常是管理包装类型的包装包作为另一个 class 的模板参数。通常使用 std::tuple
,它提供了一些方便的工具来管理类型包,但您也可以定义一个简单的模板 class/struct,如下所示(TW
用于“类型包装器”)
template <typename...>
struct TW
{ };
我提出了一个略有不同的解决方案,它是包装中性的(因此您可以与 classic 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>...
{ };
这样我们可以避免 using
在中间结构中提取 type
但 ISignalStorage
也继承自 ISignalStorageHelper
结构(我希望不是问题)。
您可以使用元组定义 ISignalStorage
个对象
ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;
自定义包装器作为 TW
ISignalStorage<foo, TW<int, double>, TW<double, int>> a;
或混合
ISignalStorage<foo, std::tuple<int, double>, TW<double, int>> a;
我需要多次继承以下class,将可变参数作为模板参数。
template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
//private
public:
void RegisterSlot(SignalAddress pSignalFunc, ISlotInvoker<ArgTypes...>* pSlotInvoker)
{
//implementation
}
};
到目前为止,我可以扩展参数包并获得多个 class 特化,但函数只接受一个参数。
template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalStorage : public ISignalMap<SignalDispatcherClass, ArgTypes>...
{
};
///////
ISignalStorage<SignalA, int, double, bool> iss;
目前,这允许我使用单个参数(相应的 int、double 或 bool)注册槽函数。 我需要的是这样的东西:
ISignalStorage<SignalA, <int, double, bool>, <int, int>, <const char*>> iss;
到目前为止,我一直在研究其他问题,其中一个似乎与主题有些接近,但我未能实施或理解它。希望有更简单的方法 (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;
}
这里的根本问题是单个参数包中没有 "squeezing" 多个可变参数包的语法。
在这种情况下,通常的方法是使用 std::tuple
来包装每个单独的参数包,并从这些元组中创建一个参数包:
ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;
然后,使用特化从 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). What I need is something that would look like:
ISignalStorage<SignalA, <int, double, bool>, <int, int>, <const char*>> iss;
正如 Sam Varshavchik 更好地解释的那样,这种类型的需求通常是管理包装类型的包装包作为另一个 class 的模板参数。通常使用 std::tuple
,它提供了一些方便的工具来管理类型包,但您也可以定义一个简单的模板 class/struct,如下所示(TW
用于“类型包装器”)
template <typename...>
struct TW
{ };
我提出了一个略有不同的解决方案,它是包装中性的(因此您可以与 classic 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>...
{ };
这样我们可以避免 using
在中间结构中提取 type
但 ISignalStorage
也继承自 ISignalStorageHelper
结构(我希望不是问题)。
您可以使用元组定义 ISignalStorage
个对象
ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;
自定义包装器作为 TW
ISignalStorage<foo, TW<int, double>, TW<double, int>> a;
或混合
ISignalStorage<foo, std::tuple<int, double>, TW<double, int>> a;