| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
// { dg-additional-options "-fconcepts" } |
| 3 |
|
| 4 |
#include <type_traits> |
| 5 |
|
| 6 |
inline constexpr bool and_impl() { return true; } |
| 7 |
|
| 8 |
template <class OperandFirst, class... OperandsRest> |
| 9 |
constexpr bool and_impl(OperandFirst operand_first, |
| 10 |
OperandsRest... operands_rest) { |
| 11 |
return operand_first && and_impl(operands_rest...); |
| 12 |
} |
| 13 |
|
| 14 |
template <class... Operands> constexpr bool and_(Operands... operands) { |
| 15 |
return and_impl(operands...); |
| 16 |
} |
| 17 |
|
| 18 |
template <class X> concept C = true; |
| 19 |
|
| 20 |
// v1 |
| 21 |
template<int, class... Xs> |
| 22 |
requires (and_(C<Xs>...)) |
| 23 |
constexpr int f(const Xs&... xs) { |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
// v2 |
| 28 |
template<int, class... Xs> |
| 29 |
constexpr int f(const Xs&... xs) { |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
int main() { |
| 34 |
static_assert(f<10>(3.0, 2.0f) == 0); |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
// 2nd example |
| 39 |
|
| 40 |
template <typename T, typename... Us> |
| 41 |
concept AreType = |
| 42 |
(std::is_same<T,Us>::value && ...); |
| 43 |
// return true; gives the same overloaded error |
| 44 |
|
| 45 |
|
| 46 |
// Function with constraint |
| 47 |
template<typename T, AreType<T>... Us> |
| 48 |
constexpr bool isValid(Us... us) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
// Function with no constraint |
| 53 |
template<typename T, typename... U> |
| 54 |
constexpr bool isValid(U... u) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
int main2() { |
| 59 |
static_assert(isValid<int>(1)); // also isValid<int>(1, 2, 3); etc |
| 60 |
return 0; |
| 61 |
} |