Search⌘ K

Define your Concepts: Equal and Ord

Learn how to define the Equal and Ord concepts in C++ templates by comparing them with Haskell's type classes. Understand how these concepts enforce type requirements for equality and ordering operations, improving type safety and constraints in generic programming.

Eq versus Equal

The Type Class Eq (Haskell)

class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool




The Concept Equal (C++)

template <typename T>
concept bool Equal(){
  return requires(T a, T b){
    { a == b } -> bool;
    { a != b } -> bool;
  };
}

Let’s have a closer look at Haskell’s type class Eq. Eq requires from its instances, that

  • they have equal == and unequal /= operation that returns a Bool.
  • both take two arguments (a -> a
...