次のコードは VS2012 でコンパイルされません
class Zot
{
public:
int A() { return 123; }
};
int _tmain(int argc, _TCHAR* argv[])
{
std::function<int (Zot*)> fn = &Zot::A;
return 0;
}
ただし、割り当てを次のように変更します。
std::function<int (Zot*)> fn = std::bind(&Zot::A, std::placeholders::_1);
動作します。
元の構文を示すオンラインの例がたくさんあります。この構文を禁止するために、C++11 仕様で何か変更がありましたか?
課題の有効な短縮形はありますか?
編集:コンパイラエラー(再現性のためにわずかに編集)は次のとおりです。
1>vc\include\functional(515): error C2664: 'std::_Func_class<_Ret,_V0_t>::_Set' : cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx,_V0_t> *'
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *
1> ]
1> and
1> [
1> _Rx=int,
1> _V0_t=Zot *
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> vc\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Do_alloc<_Myimpl,_Fret(__thiscall Zot::* const &)(void),_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *,
1> _Fret=int,
1> _Alloc=std::allocator<std::_Func_class<int,Zot *>>,
1> _Fty=int (__thiscall Zot::* const &)(void)
1> ]
1> vc\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Do_alloc<_Myimpl,_Fret(__thiscall Zot::* const &)(void),_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *,
1> _Fret=int,
1> _Alloc=std::allocator<std::_Func_class<int,Zot *>>,
1> _Fty=int (__thiscall Zot::* const &)(void)
1> ]
1> vc\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset_alloc<_Fret,Zot,std::allocator<_Ty>>(_Fret (__thiscall Zot::* const )(void),_Alloc)' being compiled
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *,
1> _Fret=int,
1> _Ty=std::_Func_class<int,Zot *>,
1> _Alloc=std::allocator<std::_Func_class<int,Zot *>>
1> ]
1> vc\include\functional(515) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset_alloc<_Fret,Zot,std::allocator<_Ty>>(_Fret (__thiscall Zot::* const )(void),_Alloc)' being compiled
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *,
1> _Fret=int,
1> _Ty=std::_Func_class<int,Zot *>,
1> _Alloc=std::allocator<std::_Func_class<int,Zot *>>
1> ]
1> vc\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset<int,Zot>(_Fret (__thiscall Zot::* const )(void))' being compiled
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *,
1> _Fret=int
1> ]
1> vc\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t>::_Reset<int,Zot>(_Fret (__thiscall Zot::* const )(void))' being compiled
1> with
1> [
1> _Ret=int,
1> _V0_t=Zot *,
1> _Fret=int
1> ]
1> c:\..\cxx11.cpp(17) : see reference to function template instantiation 'std::function<_Fty>::function<int(__thiscall Zot::* )(void)>(_Fx &&)' being compiled
1> with
1> [
1> _Fty=int (Zot *),
1> _Fx=int (__thiscall Zot::* )(void)
1> ]
1> c:\...\cxx11.cpp(17) : see reference to function template instantiation 'std::function<_Fty>::function<int(__thiscall Zot::* )(void)>(_Fx &&)' being compiled
1> with
1> [
1> _Fty=int (Zot *),
1> _Fx=int (__thiscall Zot::* )(void)
1> ]