为什么 boost::variant 不提供运算符 !=
给定两个相同的 boost::variant
实例 a
和 b
,表达式 <允许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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为它只是没有添加到库中。 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==
callsoperator==
for the actual class currently in the variant. Likewise callingoperator!=
should also calloperator!=
of the class. (Because, theoretically, a class can be defined soa!=b
is not the same as!(a==b)
.) So that would add another requirement that the classes in the variant have anoperator!=
. (There is a debate over whether you can make this assumption in the mailing list thread.)这是作者本人的答案的链接问题是在 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
因为它不需要。
Boost 有一个操作符库,它定义了操作符!=就运算符而言==
Because it doesn't need to.
Boost has an operators library which defines operator!= in term of operator==