如何用std :: function写一个指向成员函数的指针? [英] How do i write a pointer-to-member-function with std::function?

查看:331
本文介绍了如何用std :: function写一个指向成员函数的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何声明 int fn(double)在std :: function( std :: function< int(double)> )。我知道如何写一个指针到成员函数( typedef int(A :: * MemFn)(double d); )。但是如何用std :: function写一个指向成员函数的函数?

I know how to declare int fn(double) inside of std::function (std::function<int(double)>). I know how to write a pointer-to-member-function (typedef int (A::*MemFn)(double d);). But how do i write a pointer-to-member-function with std::function?

如果你喜欢编译/测试的话,可以使用Dummy代码

Dummy code if you feel like compiling/testing

-edit-基于答案,我想我只是使用typedef,而不打扰std :: function

-edit- based on answers i think i'll just use the typedef and not bother with std::function

#include <cstdio>
#include <functional>

struct A{ int fn(double){ return 0; } };
int fn2(double){ return 0; }

typedef int (A::*MemFn)(double d);
typedef std::function<int(double)> MemFn2;

void Test(A*a, MemFn2 fn){
    fn(1.2f);
}
void Test(A*a, MemFn fn){
    (a->*fn)(1.2f);
}

int main(){
    Test(new A, &A::fn);
    Test(new A, &fn2);
}


推荐答案

std :: function 完全能够直接存储成员函数指针。但是,您必须适当地调整参数列表。必须使用类型(或派生类型)的实例调用成员指针。当将它们放在 std :: function 中时,参数列表中的第一个参数应该是对象类型的指针(或引用或智能指针)。

std::function is perfectly capable of storing a member function pointer directly. However, you have to adjust the argument list appropriately. Member pointers must be called with an instance of the type (or a derived type). When putting them in a std::function, the first argument in the argument list is expected to be a pointer (or reference or smart-pointer) to the object type.

所以,如果我有以下类:

So, if I have the following class:

struct Type
{
public:
    int Foo();
};

将此成员函数存储在 std :: function 是:

The correct syntax to store this member function in a std::function is:

std::function<int(Type&)> fooCaller = &Type::Foo;

如果你想保留参数列表(在你的case, int double)),那么您需要在函数之外提供实例。这可以通过 std :: bind

If you want to preserve the argument list (in your case, int(double)), then you need to provide the instance outside of the function. This can be done via std::bind:

struct A{ int fn(double){ return 0; } };

A anInstance;
std::function<int(double)> fnCaller = std::bind(&A::fn, &anInstance, std::placeholders::_1);

请注意,这是您的责任,以确保您提供的对象指针到 std :: bind 仍然有效,只要 fnCaller 仍然有效。如果你返回 fnCaller 给某人,并且它有一个指向堆栈对象的指针,你会有麻烦。

Note that it is your responsibility to ensure that the object pointer you provide to std::bind remains alive so long as fnCaller is alive. If you return fnCaller to someone, and it has a pointer to a stack object, you're in trouble.

很好的是,由于如何定义函数调用机制,你可以将 shared_ptr (或任何可复制的智能指针)绑定为你的对象:

What's nice is that you could bind a shared_ptr (or any copyable smart pointer) as your object, thanks to how the function call mechanism is defined:

struct A{ int fn(double){ return 0; } };

auto anInstance = std::make_shared<A>();
std::function<int(double)> fnCaller = std::bind(&A::fn, anInstance, std::placeholders::_1);

现在你不必担心;因为它通过值存储 shared_ptr ,所以绑定器将继续保持对象的存活。

Now you don't have to worry; the binder will continue to keep the object alive, since it stores a shared_ptr by value.

这篇关于如何用std :: function写一个指向成员函数的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆