c++language-lawyerc++20concept

Why does same_as concept check type equality twice?


Looking at the possible implementation of the same_as concept at https://en.cppreference.com/w/cpp/concepts/same_as i noticed something strange is happening.

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>;

The first question is why a SameHelper concept is needed? The second is why same_as checks if T is the same as U and U the same as T? Isn't it redundant?


Solution

  • Interesting question. I have recently watched Andrew Sutton's talk on Concepts, and in the Q&A session someone asked the following question (timestamp in the following link): CppCon 2018: Andrew Sutton “Concepts in 60: Everything you need to know and nothing you don't”

    So the question boils down to: If I have a concept that says A && B && C, another says C && B && A, would those be equivalent? Andrew answered yes, but pointed out the fact the compiler has some internal methods (that is transparent to the user) to decompose the concepts into atomic logical propositions (atomic constraints as Andrew worded the term) and check whether they are equivalent.

    Now look at what cppreference says about std::same_as:

    std::same_as<T, U> subsumes std::same_as<U, T> and vice versa.

    It is basically an "if-and-only-if" relationship: they imply each other. (Logical Equivalence)

    My conjecture is that here the atomic constraints are std::is_same_v<T, U>. The way compilers treat std::is_same_v might make them think std::is_same_v<T, U> and std::is_same_v<U, T> as two different constraints (they are different entities!). So if you implement std::same_as using only one of them:

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

    Then std::same_as<T, U> and std::same_as<U, T> would "explode" to different atomic constrains and become not equivalent.

    Well, why does the compiler care?

    Consider this example:

    #include <type_traits>
    #include <iostream>
    #include <concepts>
    
    template< class T, class U >
    concept SameHelper = std::is_same_v<T, U>;
    
    template< class T, class U >
    concept my_same_as = SameHelper<T, U>;
    
    template< class T, class U> requires my_same_as<U, T>
    void foo(T a, U b) {
        std::cout << "Not integral" << std::endl;
    }
    
    template< class T, class U> requires (my_same_as<T, U> && std::integral<T>)
    void foo(T a, U b) {
        std::cout << "Integral" << std::endl;
    }
    
    int main() {
        foo(1, 2);
        return 0;
    }
    

    Ideally, my_same_as<T, U> && std::integral<T> subsumes my_same_as<U, T>; therefore, the compiler should select the second template specialization, except ... it does not: the compiler emits an error error: call of overloaded 'foo(int, int)' is ambiguous.

    The reason behind this is that since my_same_as<U, T> and my_same_as<T, U> does not subsume each other, my_same_as<T, U> && std::integral<T> and my_same_as<U, T> become incomparable (on the partially ordered set of constraints under the relation of subsumption).

    However, if you replace

    template< class T, class U >
    concept my_same_as = SameHelper<T, U>;
    

    with

    template< class T, class U >
    concept my_same_as = SameHelper<T, U> && SameHelper<U, T>;
    

    The code compiles.