How to write a binder3 tempalte function?

Discussion in 'C++' started by Hill, Oct 31, 2008.

  1. Hill

    Hill Guest

    This is an exercise on TCPL:
    Write a b i n d e r 3 () that binds the second and third arguments of
    a threeargument function to produce a unary predicate. Give an example
    where b i n d e r 3 () is a useful function.
    And i got a solution from << C++ solutions>>:


    template<typename FO>
    struct binder2_3
    {
    typedef typename FO::result_type result_type;
    typedef typename FO::first_argument first_argument;
    binder2_3(const FO& fo,
    typename FO::second_argument& a2,
    typename FO::third_argument& a3)
    :fo_(fo), a2_(a2), a3_(a3){}
    result_type operator()(first_argument a1){
    return fo_(a1, a2_, a3_);
    }
    private:
    FO fo_;
    const typename FO::second_argument a2_;
    const typename FO::third_argument a3_;
    };
    template<typename FO, typename P2, typename P3> inline
    binder2_3<FO> binder3(const FO& fo, const P2& a2, const P3& a3){
    return binder2_3<FO>(fo, a2, a3);
    }

    I can't understand the followingn sentence:
    typedef typename FO::result_type result_type;
    It require FO has a member named result_type ? This is a too restrict
    rule.
    Could someone give an explaination?
    Thanks
     
    Hill, Oct 31, 2008
    #1
    1. Advertising

  2. On Oct 31, 2:39 am, Hill <> wrote:
    > This is an exercise on TCPL:
    > Write a b i n d e r 3 () that binds the second and third arguments of
    > a threeargument function to produce a unary predicate. Give an example
    > where b i n d e r 3 () is a useful function.
    > And i got a solution from << C++ solutions>>:
    >
    > template<typename FO>
    > struct binder2_3
    > {
    >     typedef typename FO::result_type result_type;
    >     typedef typename FO::first_argument first_argument;
    >     binder2_3(const FO& fo,
    >               typename FO::second_argument& a2,
    >               typename FO::third_argument& a3)
    >         :fo_(fo), a2_(a2), a3_(a3){}
    >     result_type operator()(first_argument a1){
    >         return fo_(a1, a2_, a3_);
    >     }
    > private:
    >     FO fo_;
    >     const typename FO::second_argument a2_;
    >     const typename FO::third_argument a3_;};
    >
    > template<typename FO, typename P2, typename P3> inline
    > binder2_3<FO> binder3(const FO& fo, const P2& a2, const P3& a3){
    >     return binder2_3<FO>(fo, a2, a3);
    >
    > }
    >
    > I can't understand the followingn sentence:
    > typedef typename FO::result_type result_type;
    > It require FO has a member named result_type ?


    It requires FO to have a type member named result_type: typedef,
    struct, class or union.

    > This is a too restrict rule.
    > Could someone give an explaination?


    This is because your binder object wraps the call to the underlying
    functor. The binder has to return the result of the functor call,
    therefore to be able to declare the type of the return value of
    binder::eek:perator() it needs to know the type of the return value of
    the underlying functor.

    --
    Max
     
    Maxim Yegorushkin, Oct 31, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWJlbCBDaGFu?=
    Replies:
    6
    Views:
    9,452
    Rahul
    May 3, 2006
  2. Peter Hansen
    Replies:
    1
    Views:
    398
    Joona I Palaste
    Aug 24, 2003
  3. komal
    Replies:
    6
    Views:
    1,446
    msalters
    Jan 25, 2005
  4. Chuck Amadi
    Replies:
    0
    Views:
    530
    Chuck Amadi
    Jun 22, 2004
  5. Replies:
    1
    Views:
    312
    Michael DOUBEZ
    Dec 9, 2008
Loading...

Share This Page