Mem_fun_ref error

I'm having trouble finding mem_fun_ref . I must admit, I usually use functions for these kinds of things, as they can be built into speed and profit. However, this code will not be a bottleneck, so I wanted to try this thing.

Here is an example of what I want to do. I know there are other ways to do this. I do not want to use copy , I do not want to use range member functions, I do not want to use back_inserter . I specifically want to use mem_fun_ref . This is just a simple example, the real case is much more complicated. However, I really don’t know why this is wrong, but I am not familiar with mem_fun_ref or mem_fun .

Here I want to work:

 #include <list> #include <vector> #include <algorithm> #include <functional> using namespace std; int main() { list<int> a; a.push_back(1); a.push_back(2); a.push_back(3); vector<int> b; // should work like magic! for_each(a.begin(), a.end(), bind1st(mem_fun_ref(&vector<int>::push_back), b)); } 

But I get 3 errors:

 1>c:\program files\microsoft visual studio 9.0\vc\include\functional(276) : error C2529: '_Right' : reference to reference is illegal 1>c:\program files\microsoft visual studio 9.0\vc\include\functional(281) : error C2529: '_Right' : reference to reference is illegal 1>c:\program files\microsoft visual studio 9.0\vc\include\functional(282) : error C2535: 'void std::binder1st<_Fn2>::operator ()(const int &(&)) const' : member function already defined or declared 1> with 1> [ 1> _Fn2=std::mem_fun1_ref_t<void,std::vector<int>,const int &> 1> ] 1> c:\program files\microsoft visual studio 9.0\vc\include\functional(276) : see declaration of 'std::binder1st<_Fn2>::operator ()' 1> with 1> [ 1> _Fn2=std::mem_fun1_ref_t<void,std::vector<int>,const int &> 1> ] 

reference to reference is illegal makes me think that a function should take a parameter by value. But of course, this cannot be changed in vector , and it is also impossible to change in my code. Are there any simple changes to make this work? I need a solution that is 1-liner.

+4
source share
4 answers

Just use bind . The mem_fun options mem_fun too complicated.

 for_each(a.begin(), a.end(), boost::bind(&vector<int>::push_back, boost::ref(b), _1)); 

Another way that does not require the use of ref is to pass a pointer to a mutable vector:

 for_each(a.begin(), a.end(), boost::bind(&vector<int>::push_back, &b, _1)); 
+4
source

This issue was explained in Herb Sutter's "Exceptional C ++ Style", pp. 28-30. It is probably impossible to safely create a pointer to the vector<int>::push_back , since you need to be sure that the member function is accurately signed, which may not be obvious even for vector<int>::push_back in the standard library. This is because (in the standard library):

  • The signature of a member function with default parameters can be replaced with "two or more signatures of member functions with equivalent behavior."
  • Member function signatures can have additional default parameters.

Herb Sutter eventually advised that

  • Use mem_fun, just not with the standard library
  • Use pointers for member functions, just not with the standard library
+2
source

I know that you said that you did not want to use back_inserter , perhaps because you provided only simplified example code.

For someone else interested in how to do what you are trying to do and enjoy using it, use back_inserter :

 std::copy(a.begin(), a.end(), std::back_inserter(b)); 
0
source

However, there is always other_mem_fun , which I prepared before I learned about the promotion. It can fit.

0
source

Source: https://habr.com/ru/post/1285727/


All Articles