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 all2(T ...t) { return (t && ... && true); } |
| 7 | |
| 8 | int k1 = (1 + ... + 2); // expected-error {{does not contain any unexpanded parameter packs}} |
| 9 | int k2 = (1 + ...); // expected-error {{does not contain any unexpanded parameter packs}} |
| 10 | int k3 = (... + 2); // expected-error {{does not contain any unexpanded parameter packs}} |
| 11 | |
Richard Smith | 6609443 | 2016-10-20 00:55:15 +0000 | [diff] [blame] | 12 | struct A { A(int); friend A operator+(A, A); A operator-(A); A operator()(A); A operator[](A); }; |
| 13 | A operator*(A, A); |
| 14 | |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 15 | template<int ...N> void bad1() { (N + ... + N); } // expected-error {{unexpanded parameter packs in both operands}} |
| 16 | // FIXME: it would be reasonable to support this as an extension. |
| 17 | template<int ...N> void bad2() { (2 * N + ... + 1); } // expected-error {{expression not permitted as operand}} |
| 18 | template<int ...N> void bad3() { (2 + N * ... * 1); } // expected-error {{expression not permitted as operand}} |
| 19 | template<int ...N, int ...M> void bad4(int (&...x)[N]) { (N + M * ... * 1); } // expected-error {{expression not permitted as operand}} |
| 20 | template<int ...N, int ...M> void fixed4(int (&...x)[N]) { ((N + M) * ... * 1); } |
Richard Smith | 6609443 | 2016-10-20 00:55:15 +0000 | [diff] [blame] | 21 | template<typename ...T> void bad4a(T ...t) { (t * 2 + ... + 1); } // expected-error {{expression not permitted as operand}} |
| 22 | template<int ...N> void bad4b() { (A(0) + A(N) + ...); } // expected-error {{expression not permitted as operand}} |
| 23 | template<int ...N> void bad4c() { (A(0) - A(N) + ...); } // expected-error {{expression not permitted as operand}} |
| 24 | template<int ...N> void bad4d() { (A(0)(A(0)) + ... + A(0)[A(N)]); } |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 25 | |
| 26 | // Parens are mandatory. |
| 27 | template<int ...N> void bad5() { N + ...; } // expected-error {{expected expression}} expected-error +{{}} |
| 28 | template<int ...N> void bad6() { ... + N; } // expected-error {{expected expression}} |
| 29 | template<int ...N> void bad7() { N + ... + N; } // expected-error {{expected expression}} expected-error +{{}} |
| 30 | |
| 31 | // Must have a fold-operator in the relevant places. |
| 32 | template<int ...N> int bad8() { return (N + ... * 3); } // expected-error {{operators in fold expression must be the same}} |
| 33 | template<int ...N> int bad9() { return (3 + ... * N); } // expected-error {{operators in fold expression must be the same}} |
| 34 | template<int ...N> int bad10() { return (3 ? ... : N); } // expected-error +{{}} expected-note {{to match}} |
| 35 | template<int ...N> int bad11() { return (N + ... 0); } // expected-error {{expected a foldable binary operator}} expected-error {{expected expression}} |
| 36 | template<int ...N> int bad12() { return (... N); } // expected-error {{expected expression}} |
Richard Smith | 90e043d | 2017-02-15 19:57:10 +0000 | [diff] [blame] | 37 | |
| 38 | template<typename ...T> void as_operand_of_cast(int a, T ...t) { |
| 39 | return |
| 40 | (int)(a + ... + undeclared_junk) + // expected-error {{undeclared}} expected-error {{does not contain any unexpanded}} |
| 41 | (int)(t + ... + undeclared_junk) + // expected-error {{undeclared}} |
| 42 | (int)(... + undeclared_junk) + // expected-error {{undeclared}} expected-error {{does not contain any unexpanded}} |
| 43 | (int)(undeclared_junk + ...) + // expected-error {{undeclared}} |
| 44 | (int)(a + ...); // expected-error {{does not contain any unexpanded}} |
| 45 | } |
Richard Smith | fd62945 | 2017-10-31 20:29:22 +0000 | [diff] [blame] | 46 | |
| 47 | // fold-operator can be '>' or '>>'. |
| 48 | template <int... N> constexpr bool greaterThan() { return (N > ...); } |
| 49 | template <int... N> constexpr int rightShift() { return (N >> ...); } |
| 50 | |
| 51 | static_assert(greaterThan<2, 1>()); |
| 52 | static_assert(rightShift<10, 1>() == 5); |
| 53 | |
| 54 | template <auto V> constexpr auto Identity = V; |
| 55 | |
| 56 | // Support fold operators within templates. |
| 57 | template <int... N> constexpr int nestedFoldOperator() { |
| 58 | return Identity<(Identity<0> >> ... >> N)> + |
| 59 | Identity<(N >> ... >> Identity<0>)>; |
| 60 | } |
| 61 | |
| 62 | static_assert(nestedFoldOperator<3, 1>() == 1); |
Nicolas Lesser | 05141f1 | 2018-07-27 21:55:12 +0000 | [diff] [blame] | 63 | |
| 64 | // A fold-expression is a primary-expression. |
| 65 | template <typename T, typename... Ts> |
| 66 | constexpr auto castSum(Ts... Args) { |
| 67 | return (T)(Args + ...).Value; // expected-error{{member reference base type 'int' is not a structure or union}} |
| 68 | } |
| 69 | |
| 70 | template <typename... Ts> |
| 71 | constexpr auto simpleSum(Ts... Args) { |
| 72 | return (... + Args).Value; // expected-error{{member reference base type 'int' is not a structure or union}} |
| 73 | } |
| 74 | |
| 75 | void prim() { |
| 76 | castSum<int>(1, 2); |
| 77 | // expected-note@-1{{in instantiation of function template specialization}} |
| 78 | simpleSum(1, 2); |
| 79 | // expected-note@-1{{in instantiation of function template specialization}} |
| 80 | |
| 81 | struct Number { |
| 82 | int Value; |
| 83 | constexpr Number operator+(Number Rhs) const { return {Rhs.Value + Value}; } |
| 84 | }; |
| 85 | |
| 86 | static_assert(castSum<long>(Number{1}, Number{2}) == 3); |
| 87 | static_assert(simpleSum(Number{1}, Number{2}) == 3); |
| 88 | } |