传递一个成员函数,类似于c++中的std::函数(类似于c#)
Pass a member function like a std::function in C++ (like in C#)
我有一个模板类Delegate,带有一个重载的+=操作符,这使得使用Delegate类似于c#。
// ... generalized version of the template omitted from code
template<typename... TArgs>
class Delegate<void, TArgs...>
{
private:
using Func = std::function<void(TArgs...)>;
std::vector<Func> funcs;
public:
template<typename T> Delegate& operator+=(T mFunc) { funcs.push_back(Func(mFunc)); return *this; }
void operator()(TArgs... mParams) { for (auto& f : funcs) f(mParams...); }
};
这就是我要做的:
struct s
{
void test() { }
void run()
{
Delegate<void> d;
d += [] { /* do something */ ; };
d += test; // does not compile
}
};
是否有办法让d += test;
工作?
void test(int x, int y) { return x - y; }
如何编译?这个函数应该不返回任何值。返回类型为void
。
template<typename R, typename... TArgs>
class Delegate;
同样假设delegate
是一个打字错误,因为类模板是Delegate
。
无论如何,test
不返回任何东西,它编译得很好:
至于成员函数指针,您当前的实现不支持它。注意,非静态成员函数指针采用R (C::*MemPtr)(Args...) cv
的形式。
成员函数也需要一个对象的实例来处理。如果使用std::mem_fn
提取函数,可以将其作为第一个参数,还可以使用std::bind
将当前对象绑定到函数
代码表明:
struct s
{
void test() { }
void run()
{
Delegate<void> d;
d += [] { /* do something */ ; };
d += std::bind(std::mem_fn(&s::test), this); // does compile
}
};
我真的看不到d += test
的出路。你需要传递实际的对象。这个lambda版本更清楚地显示了您需要当前对象:
struct s
{
void test() { }
void run()
{
Delegate<void> d;
d += [this] { test(); };
}
};
当然,您不需要在操作符+=行中赋值。您可以修改Delegate
,使其在构造函数中接受this
,如下所示(runv1
),或者添加一个成员函数,该成员函数提供一个代理对象,该对象可以添加测试函数(runv2
):
struct s
{
void test() { }
void runv1()
{
Delegatev2<s, void> d(this);
d += test;
}
void runv2()
{
Delegate<void> d;
auto memd = d.getMemberDelegate(this);
memd += test;
}
};
正如Nawaz在edit中所说,您使用的是成员(非静态)函数,它与std::函数的void()签名不兼容。
二解:
- 使用
std::function<void(s&)>
- 将你的成员函数绑定到一个对象,
std::bind(&s::test,&s);
(像K-ballo说的,Csq做的)
此外,您应该使用rref和std::forward来实现完美的转发
相关文章:
- 类似于strcat()的函数出现问题
- 有没有类似于stoi的函数可以用于模板类
- C++ Python 中是否有类似于 help() 的函数
- 使自由函数的行为类似于成员函数 (C++)
- C++创建一个类似于 while、if 或 for 的函数
- 如何使用户定义的空默认构造函数的行为类似于编译器定义的空构造函数
- Rcpp 中"List"的操作函数 - 类似于 R 中的"Reduce function"
- 难道不应该有一个类似于"this"的与名称无关的类指针供成员函数在C++中引用吗?
- 是否有标准的C++函数,类似于地址运算符&?
- 类似于类中的 std::map 或 std::vector 的构造函数
- C++11:用容器参数定义函数(类似于基于范围的)
- 最佳 STL 变换 - 类似于三元运算符的模板函数
- 哪个函数在SFML中类似于SDL_SetColorKey()
- STL 函数类似于排序
- c++中的模函数,其行为类似于matlab中的mod
- OpenCV函数来计算类似于MATLAB产品的数组元素的乘积?
- 如何使用指针C++将类的析构函数转换为另一个类?类似于单向列表容器类和节点类
- C++:使用动态内存分配编写类似于 C realloc() 函数的函数(即更改其大小)
- 没有类似于findContours的OpenCV Cuda函数吗?
- 如何以类似于C函数的方式将dlsym映射到非静态C++成员函数