Boost Phoenix:绑定到结构的参考成员?

发布于 2024-09-16 10:37:31 字数 605 浏览 9 评论 0原文

我想使用 Boost Phoenix 生成一个 lambda 函数,用于对包含引用类型成员的结构进行 std::find_if 操作。一个人为的示例如下:

 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
  );

如果 MyStruct::x 的类型为 int 而不是 int&,则它可以正常编译。但是对于引用成员,我收到“指向引用成员的指针非法”错误。

从网上查了一下,我似乎需要使用 Phoenix 的“ref”功能,但我似乎无法弄清楚所需的语法。

有谁知道如何让它适用于“int&”类型?

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&' ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-09-23 10:37:31

抱歉,这已经太晚了,但为了将来的参考,您可以使用成员指针:

std::vector<MyStruct>::const_iterator it =
    find_if(AllStructs.begin(), AllStructs.end(),
        (&boost::phoenix::arg_names::arg1)->*&MyStruct::x == 5
    );

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
    );
冷了相思 2024-09-23 10:37:31

您无法创建指向引用成员的指针,就像无法创建指向引用的指针一样。 Daniel James 的答案仅当 x 是普通 int 而不是 int& 时才有效。请参阅凤凰。也有modules.operator.member_pointer_operator

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文