テンプレート化されたメンバー関数で std::function を使用するとコンパイル エラーが発生します。次のコードは簡単な例です。
#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;
}
そして、次のコンパイル エラーが発生します。
testtemplate.cpp: 関数 'int main()' 内:
testtemplate.cpp:29:92: エラー: 'Test::setCallback(std::_Bind_helper)(int, int), TestA, const std::_Placeholder<1>&, const std::_Placeholder の呼び出しに一致する関数がありません<2>&>::type)'</p>
testtemplate.cpp:29:92: 注: 候補は: testtemplate.cpp:10:7: 注: テンプレート void Test::setCallback(std::function)
testtemplate.cpp:10:7: 注: テンプレート引数の推定/置換に失敗しました:
testtemplate.cpp:29:92: 注: 'std::_Bind(TestA*, std::_Placeholder<1>, std::_Placeholder<2>)>' は 'std::function' から派生したものではありません</p >
C++11 と g++ 4.7 を使用しています