Debian Codegraph @

libstdc++-v3/testsuite/std/concepts/concepts.lang/concept.same/1.cc

raw
1 // Copyright (C) 2019-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do compile { target c++20 } }
19
20 #include <concepts>
21
22 static_assert( std::same_as<int, int> );
23 static_assert( !std::same_as<int&, int> );
24 static_assert( !std::same_as<int, int&> );
25 static_assert( !std::same_as<int&&, int&> );
26 static_assert( !std::same_as<const int, int> );
27 static_assert( std::same_as<const int, const int> );
28
29 struct A { };
30 static_assert( std::same_as<A, A> );
31 static_assert( !std::same_as<A, const A> );
32 static_assert( std::same_as<A const, const A> );
33 static_assert( !std::same_as<volatile A, const A> );
34
35 struct B : A { };
36 static_assert( !std::same_as<A, B> );
37
38 template<typename T, typename U>
39 constexpr int
40 check_subsumption()
41 { return 0; }
42
43 template<typename T, typename U>
44 requires std::same_as<T, U>
45 constexpr int
46 check_subsumption()
47 { return 1; }
48
49 template<typename T, typename U>
50 requires std::same_as<U, T> && std::is_const_v<T>
51 constexpr int
52 check_subsumption()
53 { return 2; }
54
55 template<typename T, typename U>
56 requires std::same_as<U, T> && std::is_volatile_v<T>
57 constexpr int
58 check_subsumption()
59 { return 3; }
60
61 static_assert( check_subsumption<short, long>() == 0 );
62 static_assert( check_subsumption<unsigned, unsigned>() == 1 );
63 // These will be ambiguous if same_as<T,U> doesn't subsume same_as<U,T>:
64 static_assert( check_subsumption<const char, const char>() == 2 );
65 static_assert( check_subsumption<volatile int, volatile int>() == 3 );