ABu
ABu

Reputation: 12259

Concept of "equal" in C++20 concepts

I have found multiple times, while reading some concepts definitions, the use of the term equal, like in Swappable:

Let t1 and t2 be equality-preserving expressions that denote distinct equal objects of type T,

Is equal defined somewhere in the standard? I guess it means that the semantics of two objects, or the value they refer (the human semantics given to their represented domain value) are the same, even if the objects are not comparable (no operator== overloaded), or something abstract like that (like, two objects a and b are equal if a == b would yield true assuming it is a valid expression --for example, because operator== is not defined because it's not required).

Upvotes: 4

Views: 632

Answers (1)

L. F.
L. F.

Reputation: 20579

Since templates are going to work on the semantics the user gives, the concept of equality is largely defined by the program. For example, if I am working with case insensitive strings, I can consider the strings FoO and fOo to be equal and FoO and bAr to be unequal. The operations I supply must reflect this semantics.

Equality isn't defined based on operator==; on the contrary, operator== is (in a sense) defined based on equality. [concept.equalitycomparable]/equality_comparable:

template<class T>
  concept equality_comparable = weakly-equality-comparable-with<T, T>;

Let a and b be objects of type T. T models equality_­comparable only if bool(a == b) is true when a is equal to b ([concepts.equality]), and false otherwise.

[ Note: The requirement that the expression a == b is equality-preserving implies that == is transitive and symmetric. — end note ]

Upvotes: 3

Related Questions