为什么 boost::variant 不提供运算符 !=

发布于 2024-07-25 06:45:03 字数 228 浏览 4 评论 0原文

给定两个相同的 boost::variant 实例 ab,表达式 <允许strong>( a == b )

然而 ( a != b ) 似乎未定义。 为什么是这样?

Given two identical boost::variant instances a and b, the expression ( a == b ) is permitted.

However ( a != b ) seems to be undefined. Why is this?

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

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

发布评论

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

评论(3

最终幸福 2024-08-01 06:45:03

我认为它只是没有添加到库中。 Boost.Operators 不会真正有帮助,因为任何一个变体都会派生自 boost::operator::equality_comparable。 David Pierre 说你可以使用它是正确的,但你的回答也是正确的,ADL 不会找到新的运算符!=,所以你需要一个 using 运算符。

我会在 boost-users 邮件列表上询问这个问题。

编辑 @AFoglia 的评论:

七个月后,我正在研究 Boost.Variant,我偶然发现了这个对省略列表的更好解释。

http://boost.org/Archives/boost/2006/06/105895.php

operator== 为当前变体中的实际类调用operator==。 同样,调用 operator!= 也应该调用该类的 operator!= 。 (因为,理论上,可以定义一个类,因此 a!=b!(a==b) 不同。)因此,这会增加另一个要求:变体中的类有一个操作符!=。 (关于是否可以在邮件列表线程中做出此假设存在争议。)

I think it's just not added to the library. The Boost.Operators won't really help, because either variant would have been derived from boost::operator::equality_comparable. David Pierre is right to say you can use that, but your response is correct too, that the new operator!= won't be found by ADL, so you'll need a using operator.

I'd ask this on the boost-users mailing list.

Edit from @AFoglia's comment:

Seven months later, and I'm studying Boost.Variant, and I stumble over this better explanation of the omission lists.

http://boost.org/Archives/boost/2006/06/105895.php

operator== calls operator== for the actual class currently in the variant. Likewise calling operator!= should also call operator!= of the class. (Because, theoretically, a class can be defined so a!=b is not the same as !(a==b).) So that would add another requirement that the classes in the variant have an operator!=. (There is a debate over whether you can make this assumption in the mailing list thread.)

じ违心 2024-08-01 06:45:03

这是作者本人的答案的链接问题是在 boost 邮件列表上提出的。

总结一下,在作者看来,实现比较运算符(!= 和 <)将对用于创建变体类型的类型增加更多要求。

但我不同意他的观点,因为 != 可以以与 == 相同的方式实现,而不必隐藏这些运算符对于构成变体的每种类型的可能实现

This is a link to the answer from the author himself when this question was formulated on boost mailing list

Summarizing it, in the author opinion, implementing comparison operators (!= and <) would add more requirements on the types used to create the variant type.

I don't agree with his point of view though, since != can be implemented in the same way as ==, without necessarily hiding the possible implementations of these operators for each of the types making up the variant

淡墨 2024-08-01 06:45:03

因为它不需要。

Boost 有一个操作符库,它定义了操作符!=就运算符而言==

Because it doesn't need to.

Boost has an operators library which defines operator!= in term of operator==

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