如何在调用时减少模板参数的数量
How to make number of template parameters less in calling?
我有一个成员函数指针作为模板参数的模板函数
template<
class Object,
class Property,
void (Object::*setProperty)(Property const&)
>
void f(Object& object, Property const& property)
每次,我都必须用3个模板参数调用函数,如下所示
f<A, Value, &A::setValue>(a, value);
使用模板参数派生或其他技术使模板参数数量减少的任何方法
f<&A::setValue>(a, value)
我的第一个想法是"不",但后来我意识到有一种奇怪的方式:
template<class Object, class Property>
struct APointlessName { //needs a better name, but I can't think of one right away
APointlessName(Object& object, Property const& property)
:object(&object), property(&property)
{}
template<void (Object::*setProperty)(Property const&)>
void with()
{
Object& object = *(this->object);
Property const& property= *(this->property);
//your function code goes here
}
Object* object;
Property const* property;
};
template< class Object, class Property>
APointlessName<Object,Property> f(Object& object, Property const& property)
{return APointlessName<Object,Property>(object, property);}
,然后这样使用:
f(a, value).with<&A::setValue>();
确实有点奇怪,但它确实避免了显式类型。希望有人能想出更好的办法。
相关文章:
- 当对话框被接受时,如何用参数调用槽
- 使用用户定义的参数调用future/async并调用类方法
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 不允许运算符 const 参数调用 const 成员函数
- 为变量模板的每个参数调用模板函数
- 如何在窗口中使用注入的 dll 中的参数调用函数
- 按引用调用与按指针参数调用的差异 前递增和后递增
- 为什么使用不匹配的参数调用重载函数仍然有效
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 基于枚举参数调用专用模板方法
- C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?
- 如何使用类模板参数来更改参数调用和函数签名?
- lambda 函数使用其参数作为模板参数调用模板函数
- Android:使用 c++ 中的 byte[] 参数调用 java 方法
- 使用不同的模板参数调用模板类的对象
- C++编译时检查是否可以用某种类型的参数调用重载函数
- 是否可以基于类模板的参数调用类方法和全局方法
- 使用单独的参数调用"boost::process::system()"时,获取"execv
- C++大括号初始值设定项作为参数调用不同的构造函数,然后预期