类定义中的可变参数模板
Variadic templates in class definitions
所以我试图在C++中围绕新的可变参数模板,我想知道我是否可以使用它来简化我拥有的一些代码。我有这样的东西:
template<typename FinalPatch,typename ReturnType, typename P1>
class Patch1: public PatchBase
{
public:
typedef Patch1<FinalPatch,ReturnType,P1> patch_type;
typedef ReturnType(WINAPI* FuncType)( P1 );
typedef std::function<ReturnType(P1)> function_type;
static ReturnType __stdcall exec( P1 p1 )
{
return _function(p1);
}
};
template<typename FinalPatch,typename ReturnType, typename P1, typename P2>
class Patch2: public PatchBase
{
public:
typedef Patch2<FinalPatch,ReturnType,P1,P2> patch_type;
typedef ReturnType(WINAPI* FuncType)( P1, P2 );
typedef std::function<ReturnType(P1,P2)> function_type;
static ReturnType __stdcall exec( P1 p1, P2 p2 )
{
return _function(p1, p2);
}
};
直到大约 10 P... 参数。
exec
静态方法用于类似Detours的系统,它替换WINAPI函数,然后将其代理给std::function
委托。
FuncType
表示"正常"函数指针。
是否可以使用可变参数模板将其简化为单个类?如果是这样,如何?
大致如下:
template<typename FinalPatch,typename ReturnType, typename... Ps>
class PatchN: public PatchBase
{
public:
typedef PatchN patch_type;
typedef ReturnType(WINAPI* FuncType)( Ps... );
typedef std::function<ReturnType(Ps...)> function_type;
// more "modern" C++11 notation
//using patch_type = PatchN;
//using FuncType = ReturnType(WINAPI*)(Ps...);
//using function_type = std::function<ReturnType(Ps...)>;
static ReturnType __stdcall exec( Ps... ps )
{
return _function(ps...);
}
// or, using perfect forwarding:
//static ReturnType __stdcall exec( Ps&&... ps )
//{
// return _function(std::forward<Ps>(ps)...);
//}
};
通常,函数类型是从单个模板参数中提取的(如std::function
),例如
template<typename FinalPatch, typename Func>
class PatchN;
template<typename FinalPatch, typename ReturnType, typename... Ps>
class PatchN<FinalPatch, ReturnType(Ps...)>;
相关文章:
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何使用可变参数模板强制转换每个变体类型
- 关于如何在具有单个参数的变体构造中选择替代方案?
- 调用参数排列不变函数 f(i++, i++)
- 参数归纳与标准::变体
- 模板化回调参数的逆变,如 C# 中的逆变
- 如何在没有参数包的情况下编写变差函数
- 通过具有嵌套类的工厂类获取多个变异类模板参数包
- 获取模板参数的成员变量值列表
- 保留短 lambda 用作函数的中间参数,使用 clang 格式保持不变
- 如何定义变体<x,y,z>提取模板参数的子类型
- 正确对齐内存模板,参数顺序不变
- 递归中不同参数类型的变元模板函数
- 通过函数指针传递给变差函数的参数会更改其值
- 提升预定义为带有参数的全局 lambda 的变体访问者
- 使用可变参数模板参数提升变体访问者
- boost ::变体 - 为什么模板参数比const字符串参数具有更高的优先级
- 将变参数包中的值加载到临时数组中
- 使用额外参数提升变体访客
- 正在将动态数组元素解析为参数?(变音符)