blob: aefee92f648a6e79df53ffa7c9c39aae29f79dd4 [file] [log] [blame]
Richard Smith0f0af192014-11-08 05:07:16 +00001// 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() {
Richard Smith0f0af192014-11-08 05:07:16 +000028 static_assert((N || ...) == false);
29 static_assert((N && ...) == true);
30 (N, ...);
31}
32template void empty<>();
33
34// An empty fold-expression isn't a null pointer just because it's an integer
Richard Smith8ee39e32016-03-04 21:27:21 +000035// with value 0. (This is no longer an issue since empty pack expansions don't
36// produce integers any more.)
Richard Smith0f0af192014-11-08 05:07:16 +000037template<int ...N> void null_ptr() {
Richard Smith8ee39e32016-03-04 21:27:21 +000038 void *p = (N || ...); // expected-error {{rvalue of type 'bool'}}
39 void *q = (N , ...); // expected-error {{rvalue of type 'void'}}
Richard Smith0f0af192014-11-08 05:07:16 +000040}
41template void null_ptr<>(); // expected-note {{in instantiation of}}
42
43template<int ...N> void bad_empty() {
Richard Smith8ee39e32016-03-04 21:27:21 +000044 (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 Smith0f0af192014-11-08 05:07:16 +000048 (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}
53template void bad_empty<>(); // expected-note {{in instantiation of}}
54
55template<int ...N> void empty_with_base() {
56 extern int k;
57 (k = ... = N); // expected-warning{{unused}}
58
Richard Smith0f0af192014-11-08 05:07:16 +000059 void (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}}
Richard Smithea97e362014-11-11 03:28:50 +000060 void ((k = ... = N));
61 (void) (k = ... = N);
Richard Smith0f0af192014-11-08 05:07:16 +000062}
63template void empty_with_base<>(); // expected-note {{in instantiation of}}
64template void empty_with_base<1>();
65
66struct A {
67 struct B {
68 struct C {
69 struct D {
70 int e;
71 } d;
72 } c;
73 } b;
74} a;
75template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) {
76 return (t.*....*ts);
77}
78static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e);