How to pass a pointer to a function function std :: function

How can I pass a function pointer to a function std::functionthrough a function. I am going to explain this 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;
}

An error message appears:

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

I just don’t understand why the function pointer cannot be passed in std::functionwhen it is passed through the function parameter. How to pass this function? I am ready to change the function parameter easy_bindfrom std::functionto the function pointer, but I really do not know how to do this.

EDIT: The question is simplified.

EDIT: Thanks to @remyabel, I was able to get what I needed: 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...);
}
+4
2

, :

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

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

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);

, std::function . - Functor.


, , , :

//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;
}

.

+2

http://en.cppreference.com/w/cpp/utility/functional/mem_fn - ,

struct Mem
{
    void MemFn() {}
};

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

All Articles