| 1 |
// { dg-do compile { target c++17 } } |
| 2 |
// { dg-options "-fconcepts" } |
| 3 |
|
| 4 |
// Test that constraint satisfaction checks work even when |
| 5 |
// processing template declarations. |
| 6 |
|
| 7 |
namespace std |
| 8 |
{ |
| 9 |
|
| 10 |
struct ostream { }; |
| 11 |
ostream cout; |
| 12 |
|
| 13 |
template<typename T> |
| 14 |
auto begin(T& t) -> decltype(t.begin()) { return t.begin(); } |
| 15 |
|
| 16 |
template<typename T> |
| 17 |
auto begin(T const& t) -> decltype(t.begin()) { return t.begin(); } |
| 18 |
|
| 19 |
template<typename T> |
| 20 |
auto end(T& t) -> decltype(t.end()) { return t.end(); } |
| 21 |
|
| 22 |
template<typename T> |
| 23 |
auto end(T const& t) -> decltype(t.end()) { return t.end(); } |
| 24 |
|
| 25 |
} // namespace std |
| 26 |
|
| 27 |
|
| 28 |
template <typename T> |
| 29 |
concept Float = __is_same_as( T, float ); |
| 30 |
|
| 31 |
template <typename T> |
| 32 |
constexpr decltype(auto) project( T t ) |
| 33 |
{ |
| 34 |
return t; |
| 35 |
} |
| 36 |
|
| 37 |
template <typename T> |
| 38 |
concept Concept = requires( T t ) { // { dg-message "in requirements" } |
| 39 |
requires Float<decltype( project(t) )>; |
| 40 |
}; |
| 41 |
|
| 42 |
template <Concept E, Concept F> |
| 43 |
constexpr decltype(auto) operator<<( E&& e, F&& f ) {} |
| 44 |
|
| 45 |
template <Concept T> |
| 46 |
void foo( T t ) |
| 47 |
{ |
| 48 |
// Try to resolve operator<< from within a template context but |
| 49 |
// with non-dependent arguments. We need to ensure that template |
| 50 |
// processing is turned off whenever checking for satisfaction. |
| 51 |
std::cout << "OK"; // { dg-error "no match" } |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
template <typename R> |
| 56 |
concept Range = |
| 57 |
requires( R r ) { |
| 58 |
requires __is_same_as( |
| 59 |
decltype(std::begin(r)), decltype(std::end(r)) ); |
| 60 |
}; |
| 61 |
|
| 62 |
struct A |
| 63 |
{ |
| 64 |
A() = default; |
| 65 |
A( const A& ) = default; |
| 66 |
|
| 67 |
// Derivation from this class forces the instantiation of |
| 68 |
// this constructor, which results in the __is_same_as type |
| 69 |
// trait above to become error_mark_node in this declaration. |
| 70 |
template <Range R> |
| 71 |
explicit A( R&& r ) { } |
| 72 |
}; |
| 73 |
|
| 74 |
struct C : A |
| 75 |
{ |
| 76 |
C() = default; |
| 77 |
C( const C& ) = default; |
| 78 |
}; |
| 79 |
|
| 80 |
int main() |
| 81 |
{ |
| 82 |
C c; // OK |
| 83 |
return 0; |
| 84 |
} |