| 1 |
// { dg-do compile { target c++11 } } |
| 2 |
|
| 3 |
template<class T> |
| 4 |
constexpr T do_last(T* x, int n) { |
| 5 |
return x[n - 1]; // |
| 6 |
} |
| 7 |
|
| 8 |
template<class T, int N> |
| 9 |
constexpr T last(T (&x)[N]) { |
| 10 |
return do_last(x, N); |
| 11 |
} |
| 12 |
|
| 13 |
constexpr bool is_negative(int x) { return x < 0; } |
| 14 |
|
| 15 |
template<class T> |
| 16 |
struct IsNegative { |
| 17 |
constexpr bool operator()(const T& x) { |
| 18 |
return x < T(0); |
| 19 |
} |
| 20 |
}; |
| 21 |
|
| 22 |
template<class T, int N, class Pred> |
| 23 |
constexpr bool has_neg(T (&x)[N], Pred p) { |
| 24 |
return p(last(x)); // Line 22 |
| 25 |
} |
| 26 |
|
| 27 |
constexpr int a[] = {1, -2}; |
| 28 |
|
| 29 |
constexpr auto answer1 = has_neg(a, IsNegative<int>{}); // Line 27 |
| 30 |
constexpr auto answer2 = has_neg(a, is_negative); |
| 31 |
|
| 32 |
static_assert(answer2 == answer1, "Error"); |