looking for explanation of bind2nd() source code

R

responsible

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
 
R

red floyd

responsible said:
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>
 
A

aman.c++

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.
 
P

puzzlecracker

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)); ?
 
R

red floyd

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top