Possible Duplicate:
Using bind1st for a method that takes argument by reference
I have the following traditional C++03 loop (using auto
for stack overflow space efficiency only):
for (auto it = some_vector.begin(); it != some_vector.end(); ++it)
{
foobar.method(*it);
}
In C++11, I managed to rewrite this into the following for_each
invocation which works perfectly well:
std::for_each(some_vector.begin(), some_vector.end(),
std::bind(&Foobar::method, std::ref(foobar), _1));
(Of course I could just use a lambda in C++11, but that's not the point.) Unfortunately, std::bind
is not part of C++03, so I tried simulating it with std::bind1st
and std::mem_fun_ref
:
std::for_each(some_vector.begin(), some_vector.end(),
std::bind1st(std::mem_fun_ref(&Foobar::method), std::ref(foobar)));
But this triggered a C2535 error in Visual Studio ("member function already defined or declared"):
// inside class binder1st in header xfunctional
result_type operator()(const argument_type& _Right) const
{ // apply functor to operands
return (op(value, _Right));
}
result_type operator()(argument_type& _Right) const
{ // apply functor to operands <--- ERROR C2535 HERE
return (op(value, _Right));
}
Is this a const-correctness bug in Visual Studio, or have I done something wrong?
Also, std::ref
does not seem to be part of C++03. Is there any workaround?