Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++1z -verify %s |
| 2 | |
| 3 | template<typename ...T> constexpr auto sum(T ...t) { return (... + t); } |
| 4 | template<typename ...T> constexpr auto product(T ...t) { return (t * ...); } |
| 5 | template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); } |
| 6 | template<typename ...T> constexpr auto dumb(T ...t) { return (false && ... && t); } |
| 7 | |
| 8 | static_assert(sum(1, 2, 3, 4, 5) == 15); |
| 9 | static_assert(product(1, 2, 3, 4, 5) == 120); |
| 10 | static_assert(!all(true, true, false, true, false)); |
| 11 | static_assert(all(true, true, true, true, true)); |
| 12 | static_assert(!dumb(true, true, true, true, true)); |
| 13 | |
| 14 | struct S { |
| 15 | int a, b, c, d, e; |
| 16 | }; |
| 17 | template<typename ...T> constexpr auto increment_all(T &...t) { |
| 18 | (++t, ...); |
| 19 | } |
| 20 | constexpr bool check() { |
| 21 | S s = { 1, 2, 3, 4, 5 }; |
| 22 | increment_all(s.a, s.b, s.c, s.d, s.e); |
| 23 | return s.a == 2 && s.b == 3 && s.c == 4 && s.d == 5 && s.e == 6; |
| 24 | } |
| 25 | static_assert(check()); |
| 26 | |
| 27 | template<int ...N> void empty() { |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 28 | static_assert((N || ...) == false); |
| 29 | static_assert((N && ...) == true); |
| 30 | (N, ...); |
| 31 | } |
| 32 | template void empty<>(); |
| 33 | |
| 34 | // An empty fold-expression isn't a null pointer just because it's an integer |
Richard Smith | 8ee39e3 | 2016-03-04 21:27:21 +0000 | [diff] [blame] | 35 | // with value 0. (This is no longer an issue since empty pack expansions don't |
| 36 | // produce integers any more.) |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 37 | template<int ...N> void null_ptr() { |
Richard Smith | 8ee39e3 | 2016-03-04 21:27:21 +0000 | [diff] [blame] | 38 | void *p = (N || ...); // expected-error {{rvalue of type 'bool'}} |
| 39 | void *q = (N , ...); // expected-error {{rvalue of type 'void'}} |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 40 | } |
| 41 | template void null_ptr<>(); // expected-note {{in instantiation of}} |
| 42 | |
| 43 | template<int ...N> void bad_empty() { |
Richard Smith | 8ee39e3 | 2016-03-04 21:27:21 +0000 | [diff] [blame] | 44 | (N + ...); // expected-error {{empty expansion for operator '+' with no fallback}} |
| 45 | (N * ...); // expected-error {{empty expansion for operator '*' with no fallback}} |
| 46 | (N | ...); // expected-error {{empty expansion for operator '|' with no fallback}} |
| 47 | (N & ...); // expected-error {{empty expansion for operator '&' with no fallback}} |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 48 | (N - ...); // expected-error {{empty expansion for operator '-' with no fallback}} |
| 49 | (N / ...); // expected-error {{empty expansion for operator '/' with no fallback}} |
| 50 | (N % ...); // expected-error {{empty expansion for operator '%' with no fallback}} |
| 51 | (N = ...); // expected-error {{empty expansion for operator '=' with no fallback}} |
| 52 | } |
| 53 | template void bad_empty<>(); // expected-note {{in instantiation of}} |
| 54 | |
| 55 | template<int ...N> void empty_with_base() { |
| 56 | extern int k; |
| 57 | (k = ... = N); // expected-warning{{unused}} |
| 58 | |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 59 | void (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}} |
Richard Smith | ea97e36 | 2014-11-11 03:28:50 +0000 | [diff] [blame] | 60 | void ((k = ... = N)); |
| 61 | (void) (k = ... = N); |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 62 | } |
| 63 | template void empty_with_base<>(); // expected-note {{in instantiation of}} |
| 64 | template void empty_with_base<1>(); |
| 65 | |
| 66 | struct A { |
| 67 | struct B { |
| 68 | struct C { |
| 69 | struct D { |
| 70 | int e; |
| 71 | } d; |
| 72 | } c; |
| 73 | } b; |
| 74 | } a; |
| 75 | template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) { |
| 76 | return (t.*....*ts); |
| 77 | } |
| 78 | static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e); |