| 1 |
// { dg-do compile { target c++17 } } |
| 2 |
// { dg-options "-fconcepts" } |
| 3 |
// { dg-skip-if "requires hosted libstdc++ for cassert" { ! hostedlib } } |
| 4 |
|
| 5 |
#include <cassert> |
| 6 |
#include <type_traits> |
| 7 |
|
| 8 |
template<typename T> |
| 9 |
concept C = __is_class(T); |
| 10 |
|
| 11 |
template<typename T> |
| 12 |
concept Type = true; |
| 13 |
|
| 14 |
struct S { }; |
| 15 |
|
| 16 |
int called; |
| 17 |
|
| 18 |
// Basic terse notation |
| 19 |
void f(auto x) { called = 1; } |
| 20 |
void g(C auto x) { called = 2; } |
| 21 |
|
| 22 |
// Overloading generic functions |
| 23 |
void h(auto x) { called = 1; } |
| 24 |
void h(C auto x) { called = 2; } |
| 25 |
|
| 26 |
void p(auto x); |
| 27 |
void p(C auto x); |
| 28 |
|
| 29 |
struct S1 { |
| 30 |
void f1(auto x) { called = 1; } |
| 31 |
void f2(C auto x) { called = 2; } |
| 32 |
|
| 33 |
void f3(auto x) { called = 1; } |
| 34 |
void f3(C auto x) { called = 2; } |
| 35 |
}; |
| 36 |
|
| 37 |
template<C T> |
| 38 |
struct S2 { |
| 39 |
void f1(auto x) { called = 1; } |
| 40 |
void f2(C auto x) { called = 2; } |
| 41 |
|
| 42 |
void f3(auto x) { called = 1; } |
| 43 |
void f3(C auto x) { called = 2; } |
| 44 |
|
| 45 |
void h1(auto x); |
| 46 |
void h2(C auto x); |
| 47 |
|
| 48 |
void h3(auto x); |
| 49 |
void h3(C auto x); |
| 50 |
|
| 51 |
template<C U> |
| 52 |
void g1(T t, U u) { called = 1; } |
| 53 |
|
| 54 |
template<C U> |
| 55 |
void g2(T t, U u); |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
void ptr(C auto *) { called = 1; } |
| 60 |
void ptr(const C auto*) { called = 2; } |
| 61 |
|
| 62 |
void ref(C auto &) { called = 1; } |
| 63 |
void ref(const C auto&) { called = 2; } |
| 64 |
|
| 65 |
void |
| 66 |
fwd_lvalue_ref(Type auto&& x) { |
| 67 |
using T = decltype(x); |
| 68 |
static_assert(std::is_lvalue_reference<T>::value, "not an lvlaue reference"); |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
fwd_const_lvalue_ref(Type auto&& x) { |
| 73 |
using T = decltype(x); |
| 74 |
static_assert(std::is_lvalue_reference<T>::value, "not an lvalue reference"); |
| 75 |
using U = typename std::remove_reference<T>::type; |
| 76 |
static_assert(std::is_const<U>::value, "not const-qualified"); |
| 77 |
} |
| 78 |
|
| 79 |
void fwd_rvalue_ref(Type auto&& x) { |
| 80 |
using T = decltype(x); |
| 81 |
static_assert(std::is_rvalue_reference<T>::value, "not an rvalue reference"); |
| 82 |
} |
| 83 |
|
| 84 |
// Make sure we can use nested names speicifers for concept names. |
| 85 |
namespace N { |
| 86 |
template<typename T> |
| 87 |
concept C = true; |
| 88 |
} // namespace N |
| 89 |
|
| 90 |
void foo(N::C auto x) { } |
| 91 |
|
| 92 |
int main() { |
| 93 |
S s; |
| 94 |
const S cs; |
| 95 |
|
| 96 |
f(0); assert(called == 1); |
| 97 |
g(s); assert(called == 2); |
| 98 |
|
| 99 |
h(0); assert(called == 1); |
| 100 |
h(s); assert(called == 2); |
| 101 |
|
| 102 |
S1 s1; |
| 103 |
s1.f1(0); assert(called == 1); |
| 104 |
s1.f2(s); assert(called == 2); |
| 105 |
|
| 106 |
s1.f3(0); assert(called == 1); |
| 107 |
s1.f3(s); assert(called == 2); |
| 108 |
|
| 109 |
S2<S> s2; |
| 110 |
s2.f1(0); assert(called == 1); |
| 111 |
s2.f2(s); assert(called == 2); |
| 112 |
|
| 113 |
s2.f3(0); assert(called == 1); |
| 114 |
s2.f3(s); assert(called == 2); |
| 115 |
|
| 116 |
s2.h1(0); assert(called == 1); |
| 117 |
s2.h2(s); assert(called == 2); |
| 118 |
|
| 119 |
s2.h3(0); assert(called == 1); |
| 120 |
s2.h3(s); assert(called == 2); |
| 121 |
|
| 122 |
s2.g1(s, s); assert(called == 1); |
| 123 |
s2.g2(s, s); assert(called == 2); |
| 124 |
|
| 125 |
ptr(&s); assert(called == 1); |
| 126 |
ptr(&cs); assert(called == 2); |
| 127 |
|
| 128 |
ref(s); assert(called == 1); |
| 129 |
ref(cs); assert(called == 2); |
| 130 |
|
| 131 |
// Check forwarding problems |
| 132 |
fwd_lvalue_ref(s); |
| 133 |
fwd_const_lvalue_ref(cs); |
| 134 |
fwd_rvalue_ref(S()); |
| 135 |
|
| 136 |
foo(0); |
| 137 |
} |
| 138 |
|
| 139 |
// Test that decl/def matching works. |
| 140 |
|
| 141 |
void p(auto x) { called = 1; } |
| 142 |
void p(C auto x) { called = 2; } |
| 143 |
|
| 144 |
template<C T> |
| 145 |
void S2<T>::h1(auto x) { called = 1; } |
| 146 |
|
| 147 |
template<C T> |
| 148 |
void S2<T>::h2(C auto x) { called = 2; } |
| 149 |
|
| 150 |
template<C T> |
| 151 |
void S2<T>::h3(auto x) { called = 1; } |
| 152 |
|
| 153 |
template<C T> |
| 154 |
void S2<T>::h3(C auto x) { called = 2; } |
| 155 |
|
| 156 |
template<C T> |
| 157 |
template<C U> |
| 158 |
void S2<T>::g2(T t, U u) { called = 2; } |