Title
Comparison templates on enumeration types
Status
open
Section
12.2.2.3 [over.match.oper]
Submitter
Tobias Loew

Created on 2023-05-11.00:00:00 last changed 11 months ago

Messages

Date: 2023-05-12.17:44:09

Suggested resolution:

Change in 12.2.2.3 [over.match.oper] bullet 3.3.4 as follows:

  • do not have the same parameter-type-list as any non-member candidate or rewritten non-member candidate that is not a function template specialization.
Date: 2023-05-11.00:00:00

Equality and relational comparisons are supported for all enumeration types, even for scoped enumerations, as specified in 12.5 [over.built] paragraph 15 and 7.6.9 [expr.rel] paragraph 6:

If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.

The comparisons can be overloaded using a non-template function, for example:

  enum class F {
    a, b
  };

  bool operator<(F,F) = delete;
  bool b = F::a < F::b;   // error, as intended

However, using a function template does not work:

  template<typename E>
  inline constexpr bool X = false;  // specialize to disable comparisons on E

  template <typename E> requires X<E>
  void operator<(E, E) = delete;

  template <>
  inline constexpr bool X<F> = true;

  bool b = F::a < F::b;             // ok, selects built-in comparison per 12.2.4.1 [over.match.best.general] bullet 2.4

The reason is that the built-in operator candidates are not suppressed if they compete with a template specialization.

History
Date User Action Args
2023-05-12 17:44:09adminsetmessages: + msg7275
2023-05-11 00:00:00admincreate