简体   繁体   English

如何将成员函数指针传递给std :: function

[英]how to pass member function pointer to std::function

How can I pass member function pointer to std::function through a function. 如何通过函数将成员函数指针传递给std::function function。 I am going to explain it by comparison ( Live Test ): 我将通过比较( 实时测试 )来解释它:

template<class R, class... FArgs, class... Args>
    void easy_bind(std::function<R(FArgs...)> f, Args&&... args){ 
}

int main() {
    fx::easy_bind(&Class::test_function, new Class);
    return 0;
}

I get an error message: 我收到一条错误消息:

no matching function for call to ‘easy_bind(void (Class::*)(int, float, std::string), Class*)’

I just don't understand why a function pointer cannot be passed to std::function when its being passed through a function parameter. 我只是不明白为什么函数指针在通过函数参数传递时不能传递给std::function How can I pass that function? 我该如何通过该功能? I am willing to change the easy_bind function parameter from std::function into a function pointer but I really don't know how. 我愿意将easy_bind函数参数从std::function更改为函数指针,但我真的不知道如何。

EDIT: Question simplified. 编辑:问题简化。

EDIT: Thanks to @remyabel, I was able to get what I needed: http://ideone.com/FtkVBg 编辑:感谢@remyabel,我得到了我需要的东西: http ://ideone.com/FtkVBg

template <typename R, typename T, typename... FArgs, typename... Args>
auto easy_bind(R (T::*mf)(FArgs...), Args&&... args)
-> decltype(fx::easy_bind(std::function<R(T*,FArgs...)>(mf), args...)) {
    return fx::easy_bind(std::function<R(T*,FArgs...)>(mf), args...);
}

http://en.cppreference.com/w/cpp/utility/functional/mem_fn is what you are supposed to use http://en.cppreference.com/w/cpp/utility/functional/mem_fn是您应该使用的

struct Mem
{
    void MemFn() {}
};

std::function<void(Mem*)> m = std::mem_fn(&Mem::MemFn);

I think the problem can be narrowed down to this: 我认为问题可以缩小到这个:

template<class R, class... FArgs>
void test(std::function<R(FArgs...)> f)
{
}

int main() {
  test(&SomeStruct::function);
}

The error message is pretty similar without the rest of the easy_bind stuff: 没有其他easy_bind东西,错误信息非常相似:

main.cpp: In function 'int main()':
main.cpp:63:31: error: no matching function for call to 
'test(void (SomeStruct::*)(int, float, std::string))'
     test(&SomeStruct::function);
main.cpp:63:31: note: candidate is:
main.cpp:49:10: note: template<class R, class ... FArgs> 
void test(std::function<_Res(_ArgTypes ...)>)
     void test(std::function<R(FArgs...)> f)
          ^
main.cpp:49:10: note:   template argument deduction/substitution failed:
main.cpp:63:31: note:   'void (SomeStruct::*)(int, float, std::string) 
{aka void (SomeStruct::*)(int, float, std::basic_string<char>)}' 
is not derived from 'std::function<_Res(_ArgTypes ...)>'
     test(&SomeStruct::function);

Essentially, it can't magically create an std::function for you. 从本质上讲,它不能为你神奇地创建一个std::function You need something like your Functor alias. 你需要像Functor别名这样的东西。


So thanks to the answer provided in generic member function pointer as a template parameter , here's what you can do: 因此,由于通用成员函数指针中提供的答案作为模板参数 ,您可以执行以下操作:

//Test Case:
struct SomeStruct {
public:
 int function(int x, float y, std::string str) {
   std::cout << x << " " << y << " " << str << std::endl;
   return 42;
 }
};

template <typename Ret, typename Struct, typename ...Args>
std::function<Ret (Struct*,Args...)> proxycall(Ret (Struct::*mf)(Args...))
{
    return std::function<Ret (Struct*,Args...)>(mf);
}

int main() {
    auto func3 = fx::easy_bind(proxycall(&SomeStruct::function), new SomeStruct);
    int ret = func3(5, 2.5, "Test3");
    std::cout << ret << "\n";

    return 0;
}

Now it works automatically. 现在它可以自动运行。

Live Example 实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM