Class equality_comparison

Synopsis

#include "breeze/operator/equality_comparison.hpp"

template< typename T >
class equality_comparison

Description

Base class template to provide equality operators (== and !=) for a derived class.

The class template equality_comparison provides operator ==() and operator !=() as namespace-scope functions whose name is associated "on demand" (i.e. at instantiation) to class T, via ADL.

Usage
  • Provide an is_equal() function for your class C, as a public and const member function.
  • Add equality_comparison< C > as a private base class of C.

As a result, == and != will be available too for class C and defined in terms of is_equal().

ADL protection
Even though this uses the Barton-Nackman trick and relies on its standard-conforming, ADL-based, mechanics (no friend injection), it is protected from ADL in namespace breeze in a way which allows only the provided operator functions to be found.

This means that the user can safely derive their own type from any of its instantiations without risk of ADL picking up anything unintended in namespace breeze. Example:


Example TODO:
Naming rationale
The name of the class template indicates the kind of comparison that its target will support. This leads to a noun-based name (as opposed, for instance, to "equality_comparable"), consistently with the general naming style of the Breeze components.

Inheritance

Decsendents: comparison

Methods

equality_comparisonProtected constructor, to enforce the usage as a base.

Source

Lines 64-95 in breeze/operator/equality_comparison.hpp.

template< typename T >
class equality_comparison
{
    // [FUTURE], [C++17], [gps]:
    //      These are all candidates for [[ nodiscard ]]
    //
    //!     \return
    //!         lhs.is_equal( rhs )
    // -----------------------------------------------------------------------
    friend bool         operator ==( T const & lhs, T const & rhs)
    {
        return lhs.is_equal( rhs ) ;
    }

    //!     \return
    //!         ! lhs.is_equal( rhs )
    // -----------------------------------------------------------------------
    friend bool         operator !=( T const & lhs, T const & rhs )
    {
        return ! lhs.is_equal( rhs ) ;
    }

protected:
    //!     Protected constructor, to enforce the usage as a base.
    //!
    //!     This used to be a protected destructor, but if a
    //!     user-declared destructor---even a defaulted one---exists,
    //!     the generation of a copy constructor is deprecated. Thus, we
    //!     switched to use a constructor, instead.
    // -----------------------------------------------------------------------
                        equality_comparison() noexcept = default ;
} ;