xentara-utils v2.0.4
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::tools::EqualityComparableWithExcept Concept Reference

Helper concept for implementing equality comparison operators. More...

#include <xentara/utils/tools/Concepts.hpp>

Concept definition

template<typename Type, typename With, typename Except>
concept xentara::utils::tools::EqualityComparableWithExcept = detail::IsEqualityComparableWithExcept<Type, With, Except>::value
Helper concept for implementing equality comparison operators.
Definition Concepts.hpp:231

Detailed Description

Helper concept for implementing equality comparison operators.

This concept can be used instead of std::equality_comparable_with as a constraint for implementing equality comparison member operators for a class. It behaves just like std::equality_comparable_with, except that it excludes a single type.

This operator is used as a contraint for the parameter of a equality comparison operator in the case where one would normally use std::equality_comparable_with. Consider a class that has a name, and the class should be comparable with anything that can be compared with that name. We would like the implementation for this class to look like this:

class MyClass
{
public:
auto operator==(const MyClass &) const -> bool = default;
template <std::equality_comparable_with<std::string> Lhs> // ERROR: constraint depends on itself
auto operator==(const Lhs &lhs) const -> bool
{
return _name == lhs;
}
private:
std::string _name;
};

Unfortunately, this will result in an error on most compilers when trying to compare two instances of MyClass, as the containt std::equality_comparable_with on operator==(const Lhs &) with try to take into account possible instantiations of operator==(const Lhs &) itself, resulting in an infinite recursion. To avoid this, MyClass can be explicitly excluded by using EqualityComparableWithExcept instead of std::equality_comparable_with:

class MyClass
{
public:
auto operator==(const MyClass &) const -> bool = default;
template <xentara::utils::tools::EqualityComparableWithExcept<std::string, MyClass> Lhs>
auto operator==(const Lhs &lhs) const -> bool
{
return _name == lhs;
}
private:
std::string _name;
};

Now, operator==(const Lhs &) can never be a cadidate for comparing two instances of MyClass, resolving the recursion.