Boost Phoenix:绑定到结构的参考成员?
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,这已经太晚了,但为了将来的参考,您可以使用成员指针:
Sorry that this is far too late, but for future reference, you can use a member pointer:
您无法创建指向引用成员的指针,就像无法创建指向引用的指针一样。 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.