Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // 困ったちゃん binder 兄弟を boost::result_of を使って少しはマシにしよう計画
- //
- #ifndef ETUDE_INCLUDED_BINDERS_HPP_
- #define ETUDE_INCLUDED_BINDERS_HPP_
- #include <boost/compressed_pair.hpp>
- #include <boost/utility/result_of.hpp>
- #include <boost/type_traits/add_reference.hpp>
- namespace etude {
- // 困ったちゃん1号
- template<class Fn, class T = typename Fn::first_argument_type>
- class binder1st
- : boost::compressed_pair<Fn, T>
- {
- typedef boost::compressed_pair<Fn, T> base;
- using base::first; using base::second;
- typedef typename boost::add_reference<T const>::type param_type;
- public:
- binder1st( Fn f, param_type x )
- : base( f, x ) {}
- template<class Signature>
- struct result;
- template<class F_, class U>
- struct result<F_(U)>
- : boost::result_of<Fn(T, U)> {};
- template<class U>
- typename boost::result_of<Fn(T, U)>::type operator()( U const& y ) {
- return first()( second(), y );
- }
- template<class U>
- typename boost::result_of<Fn(T, U)>::type operator()( U const& y ) const {
- return first()( second(), y );
- }
- };
- // 困ったちゃん2号
- template<class Fn, class U = typename Fn::second_argument_type>
- class binder2nd
- : boost::compressed_pair<Fn, U>
- {
- typedef boost::compressed_pair<Fn, U> base;
- using base::first; using base::second;
- typedef typename boost::add_reference<U const>::type param_type;
- public:
- binder2nd( Fn f, param_type y )
- : base( f, y ) {}
- template<class Signature>
- struct result;
- template<class F_, class T>
- struct result<F_(T)>
- : boost::result_of<Fn(T, U)> {};
- template<class T>
- typename boost::result_of<Fn(T, U)>::type operator()( T const& x ) {
- return first()( x, second() );
- }
- template<class T>
- typename boost::result_of<Fn(T, U)>::type operator()( T const& x ) const {
- return first()( x, second() );
- }
- };
- // ヘルパ関数
- // T, U が先なのは、参照キャプチャしたい時に
- // bind1st<hoge&>( f, x ) って書けるようにするため。
- template<class T, class Fn>
- inline binder1st<Fn, T> bind1st( Fn f, T x ) {
- return binder1st<Fn, T>( f, x );
- }
- template<class U, class Fn>
- inline binder2nd<Fn, U> bind2nd( Fn f, U y ) {
- return binder2nd<Fn, U>( f, y );
- }
- } // namespace etude
- #endif // #ifndef ETUDE_INCLUDED_BINDERS_HPP_
Add Comment
Please, Sign In to add comment