blob: 8bb79113fa9d27f2caca34e80cd426d115fdb352 [file] [log] [blame]
Stephen Hines176edba2014-12-01 14:53:08 -08001// RUN: %clang_cc1 -std=c++1z -verify %s
2
3template<typename ...T> constexpr auto sum(T ...t) { return (... + t); }
4template<typename ...T> constexpr auto product(T ...t) { return (t * ...); }
5template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); }
6template<typename ...T> constexpr auto dumb(T ...t) { return (false && ... && t); }
7
8static_assert(sum(1, 2, 3, 4, 5) == 15);
9static_assert(product(1, 2, 3, 4, 5) == 120);
10static_assert(!all(true, true, false, true, false));
11static_assert(all(true, true, true, true, true));
12static_assert(!dumb(true, true, true, true, true));
13
14struct S {
15 int a, b, c, d, e;
16};
17template<typename ...T> constexpr auto increment_all(T &...t) {
18 (++t, ...);
19}
20constexpr 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}
25static_assert(check());
26
27template<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}
36template void empty<>();
37
38// An empty fold-expression isn't a null pointer just because it's an integer
39// with value 0.
40template<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}
44template void null_ptr<>(); // expected-note {{in instantiation of}}
45
46template<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}
52template void bad_empty<>(); // expected-note {{in instantiation of}}
53
54template<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}
62template void empty_with_base<>(); // expected-note {{in instantiation of}}
63template void empty_with_base<1>();
64
65struct A {
66 struct B {
67 struct C {
68 struct D {
69 int e;
70 } d;
71 } c;
72 } b;
73} a;
74template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) {
75 return (t.*....*ts);
76}
77static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e);