c++ - 具有可变参数类型的模板多可变继承
问题描述
我需要多次继承以下类,将可变参数作为模板参数。
template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
//private
public:
void RegisterSlot(SignalAddress pSignalFunc, ISlotInvoker<ArgTypes...>* pSlotInvoker)
{
//implementation
}
};
到目前为止,我可以扩展一个参数包并获得多个类特化,但函数只接受一个参数。
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;
到目前为止,我一直在研究其他问题,其中一个似乎与该主题有些接近,尽管我未能实施或理解它。希望有一种更简单的方法(可变参数模板模板)
补充: 代码示例
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;
}
解决方案
这里的基本问题是没有语法可以从单个参数包中“挤压”多个可变参数包。
在这种情况下,通常的方法是使用 astd::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;
}
推荐阅读
- c - 最小值无法识别
- xml - 具有两个元素的两个属性的 XSD 唯一约束不起作用
- python - 使用 selenium 通过更改 xpaths 从表中获取信息
- python - sigrok-cli 无法正确通过管道传输到 C# 程序中
- typescript - 无法从 NestJS API 返回 JSON 数据
- android - Xamarin HMS Push Kit,通知消息,参数“foreground_show”被忽略?
- python-3.x - cuDNN 未能初始化可能的版本不匹配
- c++ - 对“SCIPcreate”等的未定义引用
- java - 由于redis 6现在使用多线程,现在生菜更好地使用连接池吗?
- php - 使用 jquery 刷新页面时如何激活 div 类