Firstly, I had a snippet as following:
struct D
{
int sum;
D():sum(0){accum();}
void incre(int arg){sum+=arg;}
void accum()
{
int arr[]={1,2,3,4,5};
std::for_each(arr,arr+ sizeof(arr)/sizeof(int),
std::bind1st(std::mem_fun(&D::incre),this));
cout << sum <<endl;
}
};
int main()
{
D();
}
It compiled properly.But after my changing the member function incre to
void incre(int & arg){sum+=arg;}
it produced errors, like
typename _Operation::result_type std::binder1st<_Operation>::operator()
(typename _Operation::second_argument_type&) const [with _Operation =
std::mem_fun1_t<void, D, int&>]’ cannot be overloaded
Do you have any ideas about what is going on? I’ll appreciate any help.
The problem is that internally, mem_fun tries to set as its argument type a const reference to the argument type of the member function. If you make then function take in a reference, then it tries to create a reference to a reference, which is illegal in C++. This is a known defect in the library and is being remedied by the new bind function that will appear in C++0x.