In c++20 the new three way comparison operator <=> (spaceship operator) is introduced and it can result into four values given below :
However when I run below code snippet it appears that both equal and equivalent are one and some thing, so I want to know what is the difference between them and in what scenario we should prefer one over another.
#include <iostream>
#include <compare>
int main()
{
std::strong_ordering res = (2<=>2);
if(res == std::strong_ordering::equivalent)
{
std::cout<<"Equivalent...."<<std::endl;
}
if(res == std::strong_ordering::equal)
{
std::cout<<"Equal"<<std::endl;
}
return 0;
}
O/P :
Equivalent....
Equal
They are the same thing, both numerically and conceptually. If a comparison generates strong ordering between the items being compared, equivalence and equality are the same.
The reason there are two words for it is because this is not the same for other kinds of ordering. Weak and partial don't have equality at all; they only provide equivalence.
Equivalence means that two objects can compare equal. Equality implies something stronger; if they compare equal, one can be substituted for the other in any const usage:
the property that
f(a) == f(b)
is true whenevera == b
is true, wheref
denotes a function that reads only comparison-salient state that is accessible via the argument's public const members.
If a type's comparison permits equality (which is a requirement of strong ordering), then it also permits equivalence. So for a strongly ordered comparison, they are the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With