Why did he compile the error when I added the reference argument to the member function, which is the argument to std :: mem_fun ()?

Firstly, I had a snippet as follows:

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 correctly. But after I changed the member function increto

void incre(int &  arg){sum+=arg;}

he made errors for example

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 idea what is going on? I would appreciate any help.

+3
source share
2 answers

, mem_fun -. , , , ++. bind, ++ 0x.

+4

, , bind1st mem_fun .

boost:: bind

std::for_each( arr, arr + sizeof(arr)/sizeof(int), 
   boost::bind( &D::incre, this, _1 ) );

, , GNU , , " ".

. .

, , , .

, non-const member. -.

, , std:: accumulate, std:: for_each, , , -. , , - :

Result r;
Result* binaryfunc( Result*, T value ); // returns the same pointer passed in
std::accumulate( coll.begin(), coll.end(), binaryfunc, &r );

"" . bind1st mem_fun, , .

+5

All Articles