简体   繁体   English

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

[英]Why does boost::variant not provide operator !=

Given two identical boost::variant instances a and b , the expression ( a == b ) is permitted. 给定两个相同的boost::variant实例ab ,允许表达式( a == b )

However ( a != b ) seems to be undefined. 但是( a != b )似乎未定义。 Why is this? 为什么是这样?

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. Boost.Operators实际上并没有帮助,因为任何一个变体都是从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. David Pierre说你可以使用它,但你的反应也是正确的,ADL找不到新的运算符!=所以你需要一个using运算符。

I'd ask this on the boost-users mailing list. 我在boost-users邮件列表上问这个问题。

Edit from @AFoglia's comment: 编辑来自@ AFoglia的评论:

Seven months later, and I'm studying Boost.Variant, and I stumble over this better explanation of the omission lists. 七个月后,我正在研究Boost.Variant,我偶然发现了这个遗漏名单的更好解释。

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

operator== calls operator== for the actual class currently in the variant. operator==为当前变体中的实际类调用operator== Likewise calling operator!= should also call operator!= of the class. 同样,调用operator!=也应该调用类的operator!= (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!= . (因为理论上,可以定义一个类,所以a!=b!(a==b) 。)这样就会增加另一个要求,即变量中的类有一个operator!= (There is a debate over whether you can make this assumption in the mailing list thread.) (关于你是否可以在邮件列表主题中做出这个假设存在争议。)

This is a link to the answer from the author himself when this question was formulated on boost mailing list 当这个问题在boost邮件列表中制定时,这是作者自己答案的链接

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 我不同意他的观点,因为!=可以以与==相同的方式实现,而不必为构成变体的每种类型隐藏这些运算符的可能实现

Because it doesn't need to. 因为它不需要。

Boost has an operators library which defines operator!= in term of operator== Boost有一个运算符库 ,它根据operator ==定义operator!=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM