Pete said:
Since A::foo takes one argument you have to pass two arguments to the
function call operator of the object returned by mem_fun_ref -- one for
the object to call the member function with, and one for the explicit
argument. mem_fun_ref(&A::foo)(a) tries to call it with only one
argument. And even if it took only one, you wouldn't call its operator()
from here; transform will call it.
So back up a bit. mem_fun_ref in this case returns an object whose
function call operator takes two arguments. You want to *bind* the value
'a' to the *first* argument, and pass the argument provided by transform
as the second argument. To *bind* the *first* argument use bind1st:
bind1st(mem_fun_ref(&A::foo), obj)
Thanks for the reply however the code:
#include <vector>
#include <algorithm>
#include <functional>
class B {};
class C {};
class A
{
public:
virtual C foo(const B &) const { return C(); }
};
int main()
{
using namespace std;
A obj;
A &a = obj;
vector<B> in(42);
vector<C> out(in.size());
transform(in.begin(), in.end(), out.begin(),
bind1st(mem_fun_ref(&A::foo), obj) );
}
doesn't compile either:
C:\c>g++ temp.cpp -o temp.exe
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_f
unction.h: In instantiation of
`std::binder1st<std::const_mem_fun1_ref_t<C, A, c
onst B&> >':
temp.cpp:29: instantiated from here
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_f
unction.h:406: error: forming reference to reference type `const B&'
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_f
unction.h:412: error: forming reference to reference type `const B&'
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_a
lgo.h: In function `_OutputIterator std::transform(_InputIterator,
_InputIterato
r, _OutputIterator, _UnaryOperation) [with _InputIterator =
__gnu_cxx::__normal_
iterator<B*, std::vector<B, std::allocator<B> > >, _OutputIterator =
__gnu_cxx::
__normal_iterator<C*, std::vector<C, std::allocator<C> > >,
_UnaryOperation = st
d::binder1st<std::const_mem_fun1_ref_t<C, A, const B&> >]':
temp.cpp:29: instantiated from here
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_a
lgo.h:789: error: no match for call to
`(std::binder1st<std::const_mem_fun1_ref_
t<C, A, const B&> >) (B&)'
C:\c>