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


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

/ P>

  #include & lt; Functional & gt; #include & lt; Memory & gt; Using the Std :: function; Using std :: bind; Using Std :: shared_ptr; Class test {public: template & lt; Typename T & gt; Zero set callback (function & lt; void (T, int) & gt; cb); }; Template & lt; Typename T & gt; Zero Test :: Set Callback (Function & Lt; Zero (T, Int)> CB) {none}} Class Test A {Public: Zero Testa (Int a, Int B) {}}; Int main () {Test A Test A; test test; Test.setCallback (bind (and test a :: testa, and test a, stud :: placeholder :: _ 1, stud :: placeholder :: _ 2)); Return 0; }   

and come with the following compilation error:

testtemplate.cpp: in the function "int main () one ????:

testtemplate.cpp: 29: 92: Error: There is no match for calling to call. :: Test :: setCallback (std :: _ Bind_helper) (int, int), TestA, const std :: _Placeholder & lt; 1 & gt; and; const std :: _ placeholder <2> gt; & amp; & gt; :: type)

testtemplate.cpp: 29: 92: Note: The candidate is: testtemplate .cpp: 10: 7: Note: template void test :: set callback (study :: function)

testtemplate.cpp: 10: 7: note: template arguments deduction / Q Setup failed:

testtemplate.cpp: 29: 92: Note: a. Std :: _ bind (TestA *, std :: _ Placeholder <1>, std :: _ Placeholder & <2 p>

Give different statements to find out the problem:

  Auto f = bind (& TestA :: testa, & amp; ; Test A, _1, _2); // OK Test .Set callback (F); // & lt; & Lt; --- Here is the error   

set callback set needs to know the type of T and can not get the result of it < Code> f , so give it a type

  test.setCallback & lt; TYPE & gt; (F); // TYPE: int, float, a square, ...    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -