annotate clang/test/SemaTemplate/cxx1z-fold-expressions.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 79ff65ed7e25
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -std=c++1z -verify %s
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -std=c++2a -verify %s
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 template<typename ...T> constexpr auto sum(T ...t) { return (... + t); }
anatofuz
parents:
diff changeset
5 template<typename ...T> constexpr auto product(T ...t) { return (t * ...); }
anatofuz
parents:
diff changeset
6 template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); }
anatofuz
parents:
diff changeset
7 template<typename ...T> constexpr auto dumb(T ...t) { return (false && ... && t); }
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 static_assert(sum(1, 2, 3, 4, 5) == 15);
anatofuz
parents:
diff changeset
10 static_assert(product(1, 2, 3, 4, 5) == 120);
anatofuz
parents:
diff changeset
11 static_assert(!all(true, true, false, true, false));
anatofuz
parents:
diff changeset
12 static_assert(all(true, true, true, true, true));
anatofuz
parents:
diff changeset
13 static_assert(!dumb(true, true, true, true, true));
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 struct S {
anatofuz
parents:
diff changeset
16 int a, b, c, d, e;
anatofuz
parents:
diff changeset
17 };
anatofuz
parents:
diff changeset
18 template<typename ...T> constexpr auto increment_all(T &...t) {
anatofuz
parents:
diff changeset
19 (++t, ...);
anatofuz
parents:
diff changeset
20 }
anatofuz
parents:
diff changeset
21 constexpr bool check() {
anatofuz
parents:
diff changeset
22 S s = { 1, 2, 3, 4, 5 };
anatofuz
parents:
diff changeset
23 increment_all(s.a, s.b, s.c, s.d, s.e);
anatofuz
parents:
diff changeset
24 return s.a == 2 && s.b == 3 && s.c == 4 && s.d == 5 && s.e == 6;
anatofuz
parents:
diff changeset
25 }
anatofuz
parents:
diff changeset
26 static_assert(check());
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 template<int ...N> void empty() {
anatofuz
parents:
diff changeset
29 static_assert((N || ...) == false);
anatofuz
parents:
diff changeset
30 static_assert((N && ...) == true);
anatofuz
parents:
diff changeset
31 (N, ...);
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33 template void empty<>();
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 // An empty fold-expression isn't a null pointer just because it's an integer
anatofuz
parents:
diff changeset
36 // with value 0. (This is no longer an issue since empty pack expansions don't
anatofuz
parents:
diff changeset
37 // produce integers any more.)
anatofuz
parents:
diff changeset
38 template<int ...N> void null_ptr() {
anatofuz
parents:
diff changeset
39 void *p = (N || ...); // expected-error {{rvalue of type 'bool'}}
anatofuz
parents:
diff changeset
40 void *q = (N , ...); // expected-error {{rvalue of type 'void'}}
anatofuz
parents:
diff changeset
41 }
anatofuz
parents:
diff changeset
42 template void null_ptr<>(); // expected-note {{in instantiation of}}
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 template<int ...N> void bad_empty() {
anatofuz
parents:
diff changeset
45 (N + ...); // expected-error {{empty expansion for operator '+' with no fallback}}
anatofuz
parents:
diff changeset
46 (N * ...); // expected-error {{empty expansion for operator '*' with no fallback}}
anatofuz
parents:
diff changeset
47 (N | ...); // expected-error {{empty expansion for operator '|' with no fallback}}
anatofuz
parents:
diff changeset
48 (N & ...); // expected-error {{empty expansion for operator '&' with no fallback}}
anatofuz
parents:
diff changeset
49 (N - ...); // expected-error {{empty expansion for operator '-' with no fallback}}
anatofuz
parents:
diff changeset
50 (N / ...); // expected-error {{empty expansion for operator '/' with no fallback}}
anatofuz
parents:
diff changeset
51 (N % ...); // expected-error {{empty expansion for operator '%' with no fallback}}
anatofuz
parents:
diff changeset
52 (N = ...); // expected-error {{empty expansion for operator '=' with no fallback}}
anatofuz
parents:
diff changeset
53 }
anatofuz
parents:
diff changeset
54 template void bad_empty<>(); // expected-note {{in instantiation of}}
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 template<int ...N> void empty_with_base() {
anatofuz
parents:
diff changeset
57 extern int k;
anatofuz
parents:
diff changeset
58 (k = ... = N); // expected-warning{{unused}}
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 void (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}}
anatofuz
parents:
diff changeset
61 void ((k = ... = N));
anatofuz
parents:
diff changeset
62 (void) (k = ... = N);
anatofuz
parents:
diff changeset
63 }
anatofuz
parents:
diff changeset
64 template void empty_with_base<>(); // expected-note {{in instantiation of}}
anatofuz
parents:
diff changeset
65 template void empty_with_base<1>();
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 struct A {
anatofuz
parents:
diff changeset
68 struct B {
anatofuz
parents:
diff changeset
69 struct C {
anatofuz
parents:
diff changeset
70 struct D {
anatofuz
parents:
diff changeset
71 int e;
anatofuz
parents:
diff changeset
72 } d;
anatofuz
parents:
diff changeset
73 } c;
anatofuz
parents:
diff changeset
74 } b;
anatofuz
parents:
diff changeset
75 } a;
anatofuz
parents:
diff changeset
76 template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) {
anatofuz
parents:
diff changeset
77 return (t.*....*ts);
anatofuz
parents:
diff changeset
78 }
anatofuz
parents:
diff changeset
79 static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e);
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 #if __cplusplus > 201703L
anatofuz
parents:
diff changeset
82 // The <=> operator is unique among binary operators in not being a
anatofuz
parents:
diff changeset
83 // fold-operator.
anatofuz
parents:
diff changeset
84 // FIXME: This diagnostic is not great.
anatofuz
parents:
diff changeset
85 template<typename ...T> constexpr auto spaceship1(T ...t) { return (t <=> ...); } // expected-error {{expected expression}}
anatofuz
parents:
diff changeset
86 template<typename ...T> constexpr auto spaceship2(T ...t) { return (... <=> t); } // expected-error {{expected expression}}
anatofuz
parents:
diff changeset
87 template<typename ...T> constexpr auto spaceship3(T ...t) { return (t <=> ... <=> 0); } // expected-error {{expected expression}}
anatofuz
parents:
diff changeset
88 #endif
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 // The GNU binary conditional operator ?: is not recognized as a fold-operator.
anatofuz
parents:
diff changeset
91 // FIXME: Why not? This seems like it would be useful.
anatofuz
parents:
diff changeset
92 template<typename ...T> constexpr auto binary_conditional1(T ...t) { return (t ?: ...); } // expected-error {{expected expression}}
anatofuz
parents:
diff changeset
93 template<typename ...T> constexpr auto binary_conditional2(T ...t) { return (... ?: t); } // expected-error {{expected expression}}
anatofuz
parents:
diff changeset
94 template<typename ...T> constexpr auto binary_conditional3(T ...t) { return (t ?: ... ?: 0); } // expected-error {{expected expression}}
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 namespace PR41845 {
anatofuz
parents:
diff changeset
97 template <int I> struct Constant {};
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 template <int... Is> struct Sum {
anatofuz
parents:
diff changeset
100 template <int... Js> using type = Constant<((Is + Js) + ... + 0)>; // expected-error {{pack expansion contains parameter pack 'Js' that has a different length (1 vs. 2) from outer parameter packs}}
anatofuz
parents:
diff changeset
101 };
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 Sum<1>::type<1, 2> x; // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
104 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
105
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
106 namespace PR30738 {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
107 namespace N {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
108 struct S {};
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
109 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
110
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
111 namespace T {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
112 void operator+(N::S, N::S) {}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
113 template<typename ...Ts> void f() { (Ts{} + ...); }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
114 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
115
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
116 void g() { T::f<N::S, N::S>(); }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
117
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
118 template<typename T, typename ...U> auto h(U ...v) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
119 T operator+(T, T); // expected-note {{candidate}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
120 return (v + ...); // expected-error {{invalid operands}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
121 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
122 int test_h1 = h<N::S>(1, 2, 3);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
123 N::S test_h2 = h<N::S>(N::S(), N::S(), N::S());
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
124 int test_h3 = h<struct X>(1, 2, 3);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
125 N::S test_h4 = h<struct X>(N::S(), N::S(), N::S()); // expected-note {{instantiation of}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
126 }