looking for explanation of bind2nd() source code

Discussion in 'C++' started by responsible, Aug 5, 2008.

  1. responsible

    responsible Guest

    Hi,
    Digging through the STL source code, i found this gem..

    template <class _Operation, class _Tp>
    inline binder2nd<_Operation>
    bind2nd(const _Operation& __fn, const _Tp& __x)
    {
    typedef typename _Operation::second_argument_type _Arg2_type;
    return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    }

    I am trying to understand what this does. Specifically, i don't get
    what the first two lines signify.

    Why is it that bind2nd can be called like this... bind2nd (b, t) and
    NOT bind2nd<binary_function, int> (b, t)??

    Thanks a lot in advance
    responsible, Aug 5, 2008
    #1
    1. Advertising

  2. responsible

    red floyd Guest

    responsible wrote:
    > Hi,
    > Digging through the STL source code, i found this gem..
    >
    > template <class _Operation, class _Tp>
    > inline binder2nd<_Operation>
    > bind2nd(const _Operation& __fn, const _Tp& __x)
    > {
    > typedef typename _Operation::second_argument_type _Arg2_type;
    > return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    > }
    >
    > I am trying to understand what this does. Specifically, i don't get
    > what the first two lines signify.
    >
    > Why is it that bind2nd can be called like this... bind2nd (b, t) and
    > NOT bind2nd<binary_function, int> (b, t)??
    >


    Because it's a function, and the type arguments can be deduced by the
    compiler. In fact, that's the entire purpose of bind2nd, so that the
    programmer doesn't have to provide template arguments to binder2nd.

    The first two lines:

    1. template<class _Operation, class _Tp>, declares that this is a
    function template.

    2. inline binder2nd<_Operation> states that this is an inline function,
    returning a binder2nd<_Operation>
    red floyd, Aug 5, 2008
    #2
    1. Advertising

  3. responsible

    aman.c++ Guest

    On Aug 5, 7:53 am, responsible <> wrote:
    > template <class _Operation, class _Tp>
    > inline binder2nd<_Operation>
    > bind2nd(const _Operation& __fn, const _Tp& __x)
    > {
    > typedef typename _Operation::second_argument_type _Arg2_type;
    > return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    > }
    > I am trying to understand what this does. Specifically, i don't get
    > what the first two lines signify.
    >
    > Why is it that bind2nd can be called like this... bind2nd (b, t) and
    > NOT bind2nd<binary_function, int> (b, t)??
    >


    bind2nd is a helper function that returns binder2nd function object
    adapter. (The binder2nd transforms a binary function object to
    unary ).

    The fact that we are able to call the function as bind2nd(b, t) is
    because of "Template Argument Deduction". ie. the compiler is able to
    determine the intended arguments without the programmer having to
    explicitly specify them.
    eg:
    find_if(v.begin(), v.end(), bind2nd(greater<int>(), 10));
    could also be rewritten as

    find_if(v.begin(), v.end(), bind2nd<greater<int>, int>(greater<int>(),
    10));
    This works just fine on VS2008 & gcc 3.4.4 but the code becomes
    unreadable.

    regards,
    Aman Angrish.
    aman.c++, Aug 5, 2008
    #3
  4. On Aug 5, 2:04 am, "aman.c++" <> wrote:
    > On Aug 5, 7:53 am, responsible <> wrote:
    >
    > > template <class _Operation, class _Tp>
    > > inline binder2nd<_Operation>
    > > bind2nd(const _Operation& __fn, const _Tp& __x)
    > > {
    > >   typedef typename _Operation::second_argument_type _Arg2_type;
    > >   return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    > > }
    > > I am trying to understand what this does. Specifically, i don't get
    > > what the first two lines signify.

    >
    > > Why is it that bind2nd can be called like this... bind2nd (b, t) and
    > > NOT bind2nd<binary_function, int> (b, t)??

    >
    > bind2nd is a helper function that returns binder2nd function object
    > adapter. (The binder2nd transforms a binary function object to
    > unary ).
    >
    > The fact that we are able to call the function as bind2nd(b, t) is
    > because of "Template Argument Deduction". ie. the compiler is able to
    > determine the intended arguments without the programmer having to
    > explicitly specify them.
    > eg:
    > find_if(v.begin(), v.end(), bind2nd(greater<int>(), 10));
    >    could also be rewritten as
    >
    > find_if(v.begin(), v.end(), bind2nd<greater<int>, int>(greater<int>(),
    > 10));
    >   This works just fine on VS2008 & gcc 3.4.4 but the code becomes
    > unreadable.
    >
    > regards,
    > Aman Angrish.


    How is this simalar : find_if(v.begin(), v.end(),
    bind2nd<greater<int>, int>(greater<int>(),
    10)); ?
    puzzlecracker, Aug 5, 2008
    #4
  5. responsible

    red floyd Guest

    On Aug 5, 6:27 am, puzzlecracker <> wrote:
    > On Aug 5, 2:04 am, "aman.c++" <> wrote:
    >
    >
    >
    > > On Aug 5, 7:53 am, responsible <> wrote:

    >
    > > > template <class _Operation, class _Tp>
    > > > inline binder2nd<_Operation>
    > > > bind2nd(const _Operation& __fn, const _Tp& __x)
    > > > {
    > > >   typedef typename _Operation::second_argument_type _Arg2_type;
    > > >   return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    > > > }
    > > > I am trying to understand what this does. Specifically, i don't get
    > > > what the first two lines signify.

    >
    > > > Why is it that bind2nd can be called like this... bind2nd (b, t) and
    > > > NOT bind2nd<binary_function, int> (b, t)??

    >
    > > bind2nd is a helper function that returns binder2nd function object
    > > adapter. (The binder2nd transforms a binary function object to
    > > unary ).

    >
    > > The fact that we are able to call the function as bind2nd(b, t) is
    > > because of "Template Argument Deduction". ie. the compiler is able to
    > > determine the intended arguments without the programmer having to
    > > explicitly specify them.
    > > eg:
    > > find_if(v.begin(), v.end(), bind2nd(greater<int>(), 10));
    > >    could also be rewritten as

    >
    > > find_if(v.begin(), v.end(), bind2nd<greater<int>, int>(greater<int>(),
    > > 10));
    > >   This works just fine on VS2008 & gcc 3.4.4 but the code becomes
    > > unreadable.

    >
    > > regards,
    > > Aman Angrish.

    >
    > How is this simalar : find_if(v.begin(), v.end(),
    > bind2nd<greater<int>, int>(greater<int>(),
    > 10)); ?


    It's a typo. He meant binder2nd in the second example.
    red floyd, Aug 5, 2008
    #5
    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. Jan Rendek
    Replies:
    0
    Views:
    497
    Jan Rendek
    Jul 15, 2003
  2. Michiel Salters

    Re: How to use bind2nd with mem_fun_ref?

    Michiel Salters, Jul 16, 2003, in forum: C++
    Replies:
    0
    Views:
    412
    Michiel Salters
    Jul 16, 2003
  3. red floyd
    Replies:
    1
    Views:
    1,911
    Jeff Schwab
    Dec 29, 2003
  4. John Black
    Replies:
    3
    Views:
    517
    Dave Moore
    Jun 2, 2004
  5. ES Kim

    on std::bind2nd

    ES Kim, Jun 2, 2004, in forum: C++
    Replies:
    3
    Views:
    3,572
    Leor Zolman
    Jun 2, 2004
Loading...

Share This Page