2

I would like to use Boost Phoenix to generate a lambda function for use in a std::find_if operation on a structure that contains reference-type members. A contrived example is as follows:

 struct MyStruct 
 { 
  MyStruct() : x(0) {} 
  int& x;
  };

 std::vector<MyStruct> AllStructs;

 // Search the array for an element for which x == 5
 const std::vector<MyStruct>::const_iterator& it = 
  find_if(
   AllStructs.begin(), 
   AllStructs.end(), 
   bind(&MyStruct::x, arg1) == 5
  );

If MyStruct::x is of type int instead of int&, it compiles fine. But with the reference member I get a "pointer to reference member is illegal" error.

From poking around on the net, it seems like I need to use Phoenix's 'ref' functionality, but I can't seem to figure out the required syntax.

Does anyone know how to get this to work for type 'int&' ?

Chris Kline
  • 2,249
  • 26
  • 32

2 Answers2

4

Sorry that this is far too late, but for future reference, you can use a member pointer:

std::vector<MyStruct>::const_iterator it =
    find_if(AllStructs.begin(), AllStructs.end(),
        (&boost::phoenix::arg_names::arg1)->*&MyStruct::x == 5
    );
Daniel James
  • 3,899
  • 22
  • 30
1

You cannot create a pointer to a reference member, just as you cannot create a pointer to a reference. The answer from Daniel James could work only if x was a plain int, rather than int&. See phoenix.modules.operator.member_pointer_operator also.

mabraham
  • 2,806
  • 3
  • 28
  • 25