ho un errore di compilazione quando si utilizza std :: funzione in una funzione membro basato su modelli, il codice seguente è un semplice esempio:modello di argomentazione deduzione/sostituzione fallito, quando si usa la funzione std :: e std :: bind
#include <functional>
#include <memory>
using std::function;
using std::bind;
using std::shared_ptr;
class Test {
public:
template <typename T>
void setCallback(function<void (T, int)> cb);
};
template <typename T>
void Test::setCallback(function<void (T, int)> cb)
{
// do nothing
}
class TestA {
public:
void testa(int a, int b) { }
};
int main()
{
TestA testA;
Test test;
test.setCallback(bind(&TestA::testa, &testA, std::placeholders::_1, std::placeholders::_2));
return 0;
}
e vieni con il seguente errore di compilazione:
testtemplate.cpp: In function ‘int main()’:
testtemplate.cpp:29:92: error: no matching function for call to ‘Test::setCallback(std::_Bind_helper)(int, int), TestA, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type)’
testtemplate.cpp:29:92: note: candidate is: testtemplate.cpp:10:7: note: template void Test::setCallback(std::function)
testtemplate.cpp:10:7: note: template argument deduction/substitution failed:
testtemplate.cpp:29:92: note: ‘std::_Bind(TestA*, std::_Placeholder<1>, std::_Placeholder<2>)>’ is not derived from ‘std::function’
sto usando C++ 11 e g ++ 4.7
Pronuncia 'test.setCallback (bind ...)'. Il risultato di 'bind' non è una' std :: function', quindi non c'è deduzione argomento. –
Per favore dimmi come pensi che il compilatore possa capire a cosa dedurre 'T'. – Xeo
@Xeo Sono una matricola nel template, non so come funziona la dedece. Qualche aiuto materiale? – haipeng31