c++c++20stdoptionalequality-operator

Why does std::optional have a special equality operator for operand of the type std::nullopt


The class template std::optional has the conversion constructor

constexpr optional(nullopt_t) noexcept;

So a question arises why is there declared the special single equality operator in the C++ Standard

template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;

when std::nullopt is used only as the second operand?

(See for example the C++ 20 Draft N 4860

// 20.6.7, comparison with nullopt
template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;

)

What is the reason of introducing this special operator?


Solution

  • You are looking at the C++20 draft. Drafts no later than N4820 had all the equality operators. They were later removed [likely] because of the introduction of rewritten candidates.