std::funcitonやstd::bindは、20.8.2 Requirements [func.require]で定義されているINVOKEの仕様に従う。つまり、メンバー関数やデータメンバーも扱える。
struct Foo
{
void f() { }
int x ;
} ;
int main()
{
Foo foo ;
// メンバー関数
std::function< void ( Foo & ) > f( &Foo::f ) ;
f( foo ) ; // foo.f() と同じ
// データメンバー
std::function< int & ( Foo & ) > x( &Foo::x ) ;
x( foo ) = 0 ; // foo.x = 0 と同じ
}
もしかして、意外と知られていないのだろうか。
1 comment:
データメンバーは知らなかった!
Post a Comment