template argument deduction/substitution failed, when using std::function and std::bind

后端 未结 2 369
傲寒
傲寒 2020-12-30 21:32

I have a compile error when using std::function in a templated member function, the following code is a simple example:

#include 
#include          


        
2条回答
  •  醉话见心
    2020-12-30 22:16

    To figure out the problem let separate statements:

    auto f = bind(&TestA::testa, &testA, _1, _2); // OK
    test.setCallback(f);                          // <<--- Error is here
    

    setCallback needs to know type of T and it can't deduce it from f, so give it a type

    test.setCallback(f); // TYPE: int, float, a class, ...
    

提交回复
热议问题