以可变参数方式绑定成员函数
binding member functions in a variadic fashion
我有一个参数数量可变的成员函数,存储在一个std::function
中,我想绑定实例并获取一个独立的函数对象。
template <class T, class R, class... Args>
void connect(const T& t, std::function<R(const T&, Args...)> f) {
std::function<R(Args...)> = /* bind the instance c into the function? */
}
// ...
Class c;
connect(c, &Class::foo);
对于固定数量的参数,我会使用 std::bind
,但我看不出如何为可变参数执行此操作。
我希望这就是您要实现的目标:
#include <iostream>
#include <cstdarg>
#include <functional>
class Class {
public:
void foo(...)
{
std::cout << "foo" << std::endl;
}
};
template <typename RES_T>
using ClassVarMemFunT = RES_T (Class::*)(...);
// Without typedef:
// template <class... ARGS, class CLASS_T, class RES_T>
// std::function<RES_T(ARGS...)> connect(CLASS_T& object, RES_T (CLASS_T::*funPtr)(...))
template <typename... ARGS, typename CLASS_T, typename RES_T>
std::function<RES_T(ARGS...)> connect(CLASS_T& object, ClassVarMemFunT<RES_T> funPtr)
{
std::function<RES_T(ARGS...)> resultFun = [&object, funPtr](ARGS&&... args) -> RES_T {
return (object.*funPtr)(std::forward<ARGS>(args)...);
};
return resultFun;
}
int main() {
Class c;
auto funPtr1 = connect<int, float>(c, &Class::foo);
funPtr1(10, 2.f);
auto funPtr2 = connect<double, float, int>(c, &Class::foo);
funPtr2(2., 2.f, 10);
return 0;
}
在我的实现中,connect
期望实际的参数类型作为模板参数。connect
函数返回一个需要具有ARGS...
类型的参数的std::function
。
连接有两个参数:
- 一个用于具有可变参数函数的对象(
object
)。 - 一个用于成员函数指针,指向我们要调用的可变参数函数 (
funPtr
)。
我们不能在实现中使用std::bind
(我们可以,但根据ARGS
添加尽可能多的std::placeholders
会涉及大量样板文件)。
因此,我引入了一个 lambda,它需要实际的ARGS
类型参数。我们可以将 lambda 作为std::function
返回,我们很好。
https://godbolt.org/z/7cx3rcYh8
我创建了一个可以打印出可变参数列表的版本:
https://godbolt.org/z/nMxj7Wh9j
相关文章:
- ROS 订阅回调 - 使用 boost::绑定成员函数
- 为什么我们需要 & in 绑定成员函数?
- 如何修改 std::generate 的绑定成员函数的结果
- 如何正确绑定成员函数与 boost::bind
- C2276 '&':对绑定成员函数表达式的非法操作
- 使用带有 boost::optional" 参数与 boost::spirit 绑定成员函数
- C 快速绑定成员功能的方法
- 模板类重载std::绑定成员函数
- 是否可以检测绑定成员函数引用的对象是否被删除或销毁
- 以可变参数方式绑定成员函数
- 传递绑定成员函数
- '&' :对绑定成员函数表达式的非法操作
- 绑定C++成员变量:getter/setter 或原始指针
- 使用boost绑定成员函数
- 具有静态绑定成员函数指针的可变模板的多个专业化
- "&":对绑定成员函数表达式的非法操作。从关键点矢量 OpenCV 获取容量
- 绑定成员函数的地址
- Boost.绑定成员函数并将其发布到io_service
- "!"绑定成员函数表达式的非法操作
- 绑定成员函数