先日のNestCallファンクタだが、1階層分だけ用意しておけば実はそれだけでもう、無限階層ネスト呼び出しに対応できることに気付いた。
template< class Fnc1, class Fnc2 > struct NestCall_t: public unary_function < typename Fnc1::argument_type, typename Fnc2::result_type > { NestCall_t( Fnc1 fc1, Fnc2 fc2 ) : _fc1(fc1), _fc2(fc2) {} typename Fnc2::result_type operator()( const typename Fnc1::argument_type arg ) const { return _fc2( _fc1( arg ) ); } Fnc1 _fc1; Fnc2 _fc2; }; template< class Fnc1, class Fnc2 > NestCall_t< Fnc1, Fnc2 > NestCall( Fnc1 fc1, Fnc2 fc2 ){ return NestCall_t< Fnc1, Fnc2 >( fc1, fc2 ); }
と、これさえあれば、
for each( A* ia in ca ){ ia->getB()->getC()->getD()->Hi(); }// ia
と書いておったものを、
for_each( ca.begin(), ca.end(), NestCall( mem_fun( &A::getB ), NestCall( mem_fun( &B::getC ), NestCall( mem_fun( &C::getD ), mem_fun( &D::Hi ) ) ) ) );
と書ける。->演算子に対応させることができれば、もう一つ気が利いているのだろうがなかなかその書き方はおぼつかないな-。
あ?boost::lambda でも、-> &A::Foo みたいな呼び出し方するのか、→●