VC11 と g++ 4.7.2 を使用して、次の例をコンパイルしようとしました。
#include <functional>
class X {
public:
template <typename T>
explicit X(T t)
{
std::bind(&X::invoke<T>, this, t)();
}
private:
template <typename T>
void invoke(T t)
{
t();
}
};
class Y {
public:
void foo() {
//...
}
};
int main() {
Y y;
X x(std::bind(&Y::foo, &y));
return 0;
}
しかし、それはエラーで終了しました。コンパイラの出力全体を貼り付けることが合理的かどうかはわかりませんが、一般的には
vc11 言います:
エラー C2664: 'void std::_Pmf_wrap::operator ()(_Farg0 &,_V0_t) const': パラメーター 3 を 'void' から 'std::_Bind,Y *,std::_Nil,std::_Nil に変換できません,std::_Nil,std::_Nil,std::_Nil,std::_Nil>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 ConsoleApplication1 (Microsoft Visual C++ コンパイラ Nov 2012 CTP)
および g++:
コンパイルはエラーで終了しました:
source.cpp: 'X::X(T) [with T = std::_Bind(Y*)>]' のインスタンス化中:
source.cpp:28:33: ここから必要
source.cpp :8:9: エラー: '(std::_Bind_helper(Y*)>)、X* const、std::_Bind(Y*)>&>::type {別名 std::_Bind( Y*)>)>(X*, std::_Bind(Y*)>)>}) ()'
この問題を解決する方法はありますか。私にとって非常に重要なことは、主要なアイデアを保存することです。つまり、任意の呼び出し可能なオブジェクト (関数オブジェクト、関数ポインター、または関数によって返される呼び出しラッパーstd::bind()
) でインスタンス化できるクラスです。
誰かが助けてくれたら幸いです。
PS のインスタンスを作成し、X
関数オブジェクトまたは関数ポインタを渡すとコンパイルされます。