C++应用于对象的成员函数
我想通过将成员函数作为模板参数传递来调用它,而不使用 boost 是可能的。这是我尝试做的一个示例,
class object { void method(); }
{
object object_instance;
...
apply<object:: method>();
...
template<class F>
void apply() { F(object_instance); } // want to call object_instance.F()
}
但它不起作用,所以问题是如何将对象方法绑定到对象。 谢谢
上面是一个例子,不是真正的代码。我有一堆仅名称不同但具有许多参数的函数,我想将它们包装在运算符中。
I want to call member function by passing it as template parameter, without using boost is possible. Here is an example off what I tried to do,
class object { void method(); }
{
object object_instance;
...
apply<object:: method>();
...
template<class F>
void apply() { F(object_instance); } // want to call object_instance.F()
}
that does not work, so question is how do I go about binding object method to an object.
Thanks
above is an example, not the real code. I have a bunch of functions differing only in name, but with many parameters, that I want to wrap around in operators.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
比如:
这个。
Something like:
this.
这与您的代码类似,并且允许将成员函数作为模板参数传递:
This is similar to your code, and will allow passing a member function as a template parameter:
由于成员函数不是类型或整数值,因此不能将其作为模板参数传递。您可以通过创建一个调用成员函数的结构来完成您想要的操作,尽管在模板的范围内使用 object_instance 相当难闻。
你到底想做什么?
As a member function isn't a type or integral value, you can't pass it as a template parameter. You could do what you seem to want by creating a struct which calls the member function, though having object_instance in scope in the template is rather smelly.
What are you actually trying to do?
此示例适用于 c++ox 中的右值引用。不适用于所有编译器。
This example is for rvalue references in c++ox. Does not work on all compilers.