1 // RUN: %clang_cc1 -std=c++1z -verify %s
2
sum(T...t)3 template<typename ...T> constexpr auto sum(T ...t) { return (... + t); }
product(T...t)4 template<typename ...T> constexpr auto product(T ...t) { return (t * ...); }
all(T...t)5 template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); }
dumb(T...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 };
increment_all(T &...t)17 template<typename ...T> constexpr auto increment_all(T &...t) {
18 (++t, ...);
19 }
check()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
empty()27 template<int ...N> void empty() {
28 static_assert((N + ...) == 0);
29 static_assert((N * ...) == 1);
30 static_assert((N | ...) == 0);
31 static_assert((N & ...) == -1);
32 static_assert((N || ...) == false);
33 static_assert((N && ...) == true);
34 (N, ...);
35 }
36 template void empty<>();
37
38 // An empty fold-expression isn't a null pointer just because it's an integer
39 // with value 0.
null_ptr()40 template<int ...N> void null_ptr() {
41 void *p = (N + ...); // expected-error {{rvalue of type 'int'}}
42 void *q = (N | ...); // expected-error {{rvalue of type 'int'}}
43 }
44 template void null_ptr<>(); // expected-note {{in instantiation of}}
45
bad_empty()46 template<int ...N> void bad_empty() {
47 (N - ...); // expected-error {{empty expansion for operator '-' with no fallback}}
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 }
52 template void bad_empty<>(); // expected-note {{in instantiation of}}
53
empty_with_base()54 template<int ...N> void empty_with_base() {
55 extern int k;
56 (k = ... = N); // expected-warning{{unused}}
57
58 void (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}}
59 void ((k = ... = N));
60 (void) (k = ... = N);
61 }
62 template void empty_with_base<>(); // expected-note {{in instantiation of}}
63 template void empty_with_base<1>();
64
65 struct A {
66 struct B {
67 struct C {
68 struct D {
69 int e;
70 } d;
71 } c;
72 } b;
73 } a;
apply(T & t,Ts...ts)74 template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) {
75 return (t.*....*ts);
76 }
77 static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e);
78