2

Looking the "possible" implementation section of std::same_as something strikes me as odd:

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 check for type equality is done twice, once as T==U and the second as U==T. Given that equailty is symmetric by nature, why is the extra check needed?

Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
  • 2
    "equality is symmetric by nature" but the compiler doesn't (and can't) know that. Hence manually "symmetrizing" it. – HTNW May 15 '21 at 21:50
  • @HTNW It can know that `std::is_same` is symmetric. This isn't using `operator==` which should be (but isn't necessarily) symmetric, this is `std::is_same` which genuinely is. – Nathan Pierson May 15 '21 at 21:52
  • @NathanPierson exactly, internally `std::is_same` is used which is implemented as a class template specialization where the two arguments are the same type; reversing the order does nothing to the result. – Lorah Attkins May 15 '21 at 21:54
  • I'll try and grasp the answers in the linked question. Maybe my previous comment is ill informed. – Lorah Attkins May 15 '21 at 21:57
  • 2
    @NathanPierson But it's not going to do that analysis by itself because it's far, far too complicated in general ("doesn't know") and (because of that) is not allowed to by the standard anyway ("and can't") (*and* to some extent it isn't truly symmetric: while the standard `is_same` cannot be user-specialized, the exact same definition made by a user can always have a "broken" equation added). But decomposing and checking equivalence between expressions in a purely syntactic way is much more tractable, so the knowledge that "equality is symmetric" is encoded at that level. – HTNW May 15 '21 at 21:57
  • 1
    The compiler only performs very basic analysis on the concepts (when checking for symmety and similar things): it understands `&&`, `||`, parentheses, and it can look inside of concepts. Everything else is considered opaque. – HolyBlackCat May 15 '21 at 21:59

0 Answers0