llvm-project
94 строки · 4.9 Кб
1// 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 all2(T ...t) { return (t && ... && true); }7
8int k1 = (1 + ... + 2); // expected-error {{does not contain any unexpanded parameter packs}}9int k2 = (1 + ...); // expected-error {{does not contain any unexpanded parameter packs}}10int k3 = (... + 2); // expected-error {{does not contain any unexpanded parameter packs}}11
12struct A { A(int); friend A operator+(A, A); A operator-(A); A operator()(A); A operator[](A); };13A operator*(A, A);14
15template<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.
17template<int ...N> void bad2() { (2 * N + ... + 1); } // expected-error {{expression not permitted as operand}}18template<int ...N> void bad3() { (2 + N * ... * 1); } // expected-error {{expression not permitted as operand}}19template<int ...N, int ...M> void bad4(int (&...x)[N]) { (N + M * ... * 1); } // expected-error {{expression not permitted as operand}}20template<int ...N, int ...M> void fixed4(int (&...x)[N]) { ((N + M) * ... * 1); }21template<typename ...T> void bad4a(T ...t) { (t * 2 + ... + 1); } // expected-error {{expression not permitted as operand}}22template<int ...N> void bad4b() { (A(0) + A(N) + ...); } // expected-error {{expression not permitted as operand}}23template<int ...N> void bad4c() { (A(0) - A(N) + ...); } // expected-error {{expression not permitted as operand}}24template<int ...N> void bad4d() { (A(0)(A(0)) + ... + A(0)[A(N)]); }25
26// Parens are mandatory.
27template<int ...N> void bad5() { N + ...; } // expected-error {{expected expression}} expected-error +{{}}28template<int ...N> void bad6() { ... + N; } // expected-error {{expected expression}}29template<int ...N> void bad7() { N + ... + N; } // expected-error {{expected expression}} expected-error +{{}}30
31// Must have a fold-operator in the relevant places.
32template<int ...N> int bad8() { return (N + ... * 3); } // expected-error {{operators in fold expression must be the same}}33template<int ...N> int bad9() { return (3 + ... * N); } // expected-error {{operators in fold expression must be the same}}34template<int ...N> int bad10() { return (3 ? ... : N); } // expected-error +{{}} expected-note {{to match}}35template<int ...N> int bad11() { return (N + ... 0); } // expected-error {{expected a foldable binary operator}} expected-error {{expected expression}}36template<int ...N> int bad12() { return (... N); } // expected-error {{expected expression}}37
38template<typename ...T> void as_operand_of_cast(int a, T ...t) {39return40(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(int)(a, ...) + // expected-error {{does not contain any unexpanded}}46(int)(..., a) + // expected-error {{does not contain any unexpanded}}47(int)(a, ..., undeclared_junk) + // expected-error {{undeclared}} expected-error {{does not contain any unexpanded}}48(int)(t, ...) +49(int)(..., t) +50(int)(t, ..., a);51}
52
53// fold-operator can be '>' or '>>'.
54template <int... N> constexpr bool greaterThan() { return (N > ...); }55template <int... N> constexpr int rightShift() { return (N >> ...); }56
57static_assert(greaterThan<2, 1>());58static_assert(rightShift<10, 1>() == 5);59
60template <auto V> constexpr auto Identity = V;61
62// Support fold operators within templates.
63template <int... N> constexpr int nestedFoldOperator() {64return Identity<(Identity<0> >> ... >> N)> +65Identity<(N >> ... >> Identity<0>)>;66}
67
68static_assert(nestedFoldOperator<3, 1>() == 1);69
70// A fold-expression is a primary-expression.
71template <typename T, typename... Ts>72constexpr auto castSum(Ts... Args) {73return (T)(Args + ...).Value; // expected-error{{member reference base type 'int' is not a structure or union}}74}
75
76template <typename... Ts>77constexpr auto simpleSum(Ts... Args) {78return (... + Args).Value; // expected-error{{member reference base type 'int' is not a structure or union}}79}
80
81void prim() {82castSum<int>(1, 2);83// expected-note@-1{{in instantiation of function template specialization}}84simpleSum(1, 2);85// expected-note@-1{{in instantiation of function template specialization}}86
87struct Number {88int Value;89constexpr Number operator+(Number Rhs) const { return {Rhs.Value + Value}; }90};91
92static_assert(castSum<long>(Number{1}, Number{2}) == 3);93static_assert(simpleSum(Number{1}, Number{2}) == 3);94}
95