Re: bind2nd and a const reference.
So, if I were to define a new xbind2nd and xbinder2nd as below:
// TEMPLATE CLASS binder2nd
template<class _Fn2>
class xbinder2nd
: public std::unary_function<typename
_Fn2::first_argument_type,
typename _Fn2::result_type>
{ // functor adapter _Func(left, stored)
public:
typedef std::unary_function<typename
_Fn2::first_argument_type,
typename _Fn2::result_type> _Base;
typedef typename _Base::argument_type argument_type;
typedef typename _Base::result_type result_type;
xbinder2nd(const _Fn2& _Func,
typename _Fn2::second_argument_type _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}
result_type operator()(const argument_type& _Left) const
{ // apply functor to operands
return (op(_Left, value));
}
result_type operator()(argument_type& _Left) const
{ // apply functor to operands
return (op(_Left, value));
}
protected:
_Fn2 op; // the functor to apply
typename _Fn2::second_argument_type value; // the right
operand
};
template<class _Fn2, class _Ty> inline
xbinder2nd<_Fn2> xbind2nd(const _Fn2& _Func, const _Ty&
_Right)
{ // return a binder2nd functor adapter
typename _Fn2::second_argument_type _Val(_Right);
return (xbinder2nd<_Fn2>(_Func, _Val));
}
Then it should work fine. Seems like a lot of work since I could just
use a for() loop. So, unless there's some improvement by using this
code, I don't think I'll make things more complicated by using it.
Is there a reason binder2nd was defined that way? I would think that
if somebody wanted it passed as a const-reference, they would have
defined their function to accept one as I did.
Thanks,
PaulH
On Jul 11, 4:57 am, Alex Blekhman <tkfx.REM...@yahoo.com> wrote:
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