Re: bind2nd and a const reference.
PaulH wrote:
I'm trying to use the for_each(), bind2nd(), mem_fun() combination
below to perform a for loop, but I get the compiler warnings and error
below.
If I replace the Update( const T& hint ) function with Update( T
hint ), everything compiles just fine. Or, if I replace the for_each()
loop with a for() loop, everything works.
Why doesn't the for_each() loop work with the constant-reference
version of the update function?
It is because of `std::binder2nd' constructor. Lets' expand
the types involved for this statement:
std::for_each(
mylist.begin(), mylist.end(),
std::bind2nd(
std::mem_fun(&myclass::Update)
);
"std::mem_fun(&myclass::Update)" returns following class:
std::mem_fun1_t<
Result = void,
Type = myclass,
Arg = const int&
>
`mem_fun1_t' is derived from `binary_function', so following
typenames are introduced:
mem_fun1_t::first_argument_type = myclass*
mem_fun1_t::second_argument_type = const int&
mem_fun1_t::result_type = void
Then `mem_fun1_t' object is passed to `std::bind2nd'
function, which attempts to create following class:
binder2nd<
Operation = std::binary_function<
myclass*,
const int&,
void
>
>
`std::binder2nd' has following constructor:
binder2nd(
const Operation& _Func,
const typename Operation::second_argument_type& _Right
);
Now, try to substitute typenames with actual types:
binder2nd(
const binary_function<...>& _Func,
const const int& & _Right)
^^^^^^^^^^
This is `mem_fun1_t::second_argument_type'.
That's why you get error C2529: '_Right' : reference to
reference is illegal.
Alex