c++standardsc++20c++-conceptslanguage-implementation

Why is std::same_as implemented in such a weird way?


cppref gives a possible implementation of std::same_as:

namespace detail {
    template<class T, class U>
    concept SameHelper = std::is_same_v<T, U>;
}

template<class T, class U>
concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;

Why is it not implemented just as follows:

template<class T, class U>
concept same_as = std::is_same_v<T, U> && std::is_same_v<U, T>;

or even shorter:

template<class T, class U>
concept same_as = std::is_same_v<T, U>;

Solution

  • It is t handle subsumption which only happens with concepts.

    With your proposal,

    same_as<T, U> doesn't subsume same_as<U, T>.

    Further reading in cppreference.