| 1 |
// { dg-do compile { target c++17 } } |
| 2 |
// { dg-options "-fconcepts" } |
| 3 |
|
| 4 |
template<typename T, typename... Params> |
| 5 |
concept HasInit = requires(T t, Params... p) { t.init(p...); }; |
| 6 |
|
| 7 |
struct Initable { void init(int) { } }; |
| 8 |
struct Createable { void create(int) { } }; |
| 9 |
|
| 10 |
struct Foo{ |
| 11 |
template<typename CB> |
| 12 |
void for_each(CB&& cb) |
| 13 |
{ |
| 14 |
Initable i; |
| 15 |
Createable c; |
| 16 |
cb(i); |
| 17 |
cb(c); |
| 18 |
} |
| 19 |
|
| 20 |
Foo() |
| 21 |
{ |
| 22 |
struct Bar { int x; }; |
| 23 |
for_each( |
| 24 |
[&](auto& foo){ |
| 25 |
if constexpr (HasInit<decltype(foo), int>) |
| 26 |
{ |
| 27 |
foo.init(5); |
| 28 |
} |
| 29 |
}); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
int main() |
| 34 |
{ |
| 35 |
Foo f; |
| 36 |
return 0; |
| 37 |
} |