| 1 | // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -verify %s |
| 2 | // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -verify %s -DCLASS |
| 3 | |
| 4 | #ifdef CLASS |
| 5 | struct Outer { |
| 6 | #endif |
| 7 | |
| 8 | template<typename> struct A {}; |
| 9 | |
| 10 | // Valid forms. |
| 11 | A(int(&)[1]) -> A<int>; |
| 12 | explicit A(int(&)[2]) -> A<int>; |
| 13 | |
| 14 | // Declarator pieces are not OK. |
| 15 | *A(int(&)[3]) -> A<int>; // expected-error {{cannot specify any part of a return type in the declaration of a deduction guide}} |
| 16 | &A(int(&)[4]) -> A<int>; // expected-error {{cannot specify any part of a return type in the declaration of a deduction guide}} |
| 17 | A(int(&)[5])[3] -> A<int>; |
| 18 | #ifdef CLASS // FIXME: These diagnostics are both pretty bad. |
| 19 | // expected-error@-2 {{function cannot return array type}} expected-error@-2 {{';'}} |
| 20 | #else |
| 21 | // expected-error@-4 {{expected function body after function declarator}} |
| 22 | #endif |
| 23 | |
| 24 | (A[3])(int(&)[5][1]) -> A<int>; // expected-error {{'<deduction guide for A>' cannot be the name of a variable}} |
| 25 | #ifndef CLASS |
| 26 | // expected-error@-2 {{declared as array of functions}} |
| 27 | #endif |
| 28 | (*A)(int(&)[5][2]) -> A<int>; // expected-error {{'<deduction guide for A>' cannot be the name of a variable}} |
| 29 | (&A)(int(&)[5][3]) -> A<int>; // expected-error {{'<deduction guide for A>' cannot be the name of a variable}} |
| 30 | (*A(int))(int(&)[5][4]) -> A<int>; // expected-error {{cannot specify any part of a return type in the declaration of a deduction guide}} |
| 31 | |
| 32 | // (Pending DR) attributes and parens around the declarator-id are OK. |
| 33 | [[deprecated]] A(int(&)[6]) [[]] -> A<int> [[]]; |
| 34 | A [[]] (int(&)[7]) -> A<int>; |
| 35 | (A)(int(&)[8]) -> A<int>; |
| 36 | |
| 37 | // ... but the trailing-return-type is part of the function-declarator as normal |
| 38 | (A(int(&)[9])) -> A<int>; |
| 39 | #ifdef CLASS // FIXME: These diagnostics are both pretty bad. |
| 40 | // expected-error@-2 {{deduction guide declaration without trailing return type}} expected-error@-2 {{';'}} |
| 41 | #else |
| 42 | // expected-error@-4 {{expected function body after function declarator}} |
| 43 | #endif |
| 44 | (A(int(&)[10]) -> A<int>); // expected-error {{trailing return type may not be nested within parentheses}} |
| 45 | |
| 46 | // A trailing-return-type is mandatory. |
| 47 | A(int(&)[11]); // expected-error {{deduction guide declaration without trailing return type}} |
| 48 | |
| 49 | // No type specifier is permitted; we don't even parse such cases as a deduction-guide. |
| 50 | int A(int) -> A<int>; // expected-error {{function with trailing return type must specify return type 'auto', not 'int'}} |
| 51 | template<typename T> struct B {}; // expected-note {{here}} |
| 52 | auto B(int) -> B<int>; // expected-error {{redefinition of 'B' as different kind of symbol}} |
| 53 | |
| 54 | // No storage class specifier, function specifier, ... |
| 55 | friend A(int(&)[20]) -> A<int>; |
| 56 | #ifdef CLASS |
| 57 | // expected-error@-2 {{cannot declare a deduction guide as a friend}} |
| 58 | #else |
| 59 | // expected-error@-4 {{'friend' used outside of class}} |
| 60 | #endif |
| 61 | typedef A(int(&)[21]) -> A<int>; // expected-error {{deduction guide cannot be declared 'typedef'}} |
| 62 | constexpr A(int(&)[22]) -> A<int>; // expected-error {{deduction guide cannot be declared 'constexpr'}} |
| 63 | inline A(int(&)[23]) -> A<int>; // expected-error {{deduction guide cannot be declared 'inline'}} |
| 64 | static A(int(&)[24]) -> A<int>; // expected-error {{deduction guide cannot be declared 'static'}} |
| 65 | thread_local A(int(&)[25]) -> A<int>; // expected-error {{'thread_local' is only allowed on variable declarations}} |
| 66 | extern A(int(&)[26]) -> A<int>; |
| 67 | #ifdef CLASS |
| 68 | // expected-error@-2 {{storage class specified for a member}} |
| 69 | #else |
| 70 | // expected-error@-4 {{deduction guide cannot be declared 'extern'}} |
| 71 | #endif |
| 72 | mutable A(int(&)[27]) -> A<int>; // expected-error-re {{{{'mutable' cannot be applied to|illegal storage class on}} function}} |
| 73 | virtual A(int(&)[28]) -> A<int>; // expected-error {{'virtual' can only appear on non-static member functions}} |
| 74 | const A(int(&)[28]) -> A<int>; // expected-error {{deduction guide cannot be declared 'const'}} |
| 75 | |
| 76 | const volatile static constexpr inline A(int(&)[29]) -> A<int>; // expected-error {{deduction guide cannot be declared 'static inline constexpr const volatile'}} |
| 77 | |
| 78 | A(int(&)[30]) const -> A<int>; // expected-error {{deduction guide cannot have 'const' qualifier}} |
| 79 | |
| 80 | // No definition is allowed. |
| 81 | A(int(&)[40]) -> A<int> {} // expected-error {{deduction guide cannot have a function definition}} |
| 82 | A(int(&)[41]) -> A<int> = default; // expected-error {{deduction guide cannot have a function definition}} expected-error {{only special member functions may be defaulted}} |
| 83 | A(int(&)[42]) -> A<int> = delete; // expected-error {{deduction guide cannot have a function definition}} |
| 84 | A(int(&)[43]) -> A<int> try {} catch (...) {} // expected-error {{deduction guide cannot have a function definition}} |
| 85 | |
| 86 | #ifdef CLASS |
| 87 | }; |
| 88 | #endif |
| 89 | |
| 90 | namespace ExplicitInst { |
| 91 | // Explicit instantiation / specialization is not permitted. |
| 92 | template<typename T> struct B {}; |
| 93 | template<typename T> B(T) -> B<T>; |
| 94 | template<> B(int) -> B<int>; // expected-error {{deduction guide cannot be explicitly specialized}} |
| 95 | extern template B(float) -> B<float>; // expected-error {{deduction guide cannot be explicitly instantiated}} |
| 96 | template B(char) -> B<char>; // expected-error {{deduction guide cannot be explicitly instantiated}} |
| 97 | |
| 98 | // An attempt at partial specialization doesn't even parse as a deduction-guide. |
| 99 | template<typename T> B<T*>(T*) -> B<T*>; // expected-error 1+{{}} expected-note 0+{{}} |
| 100 | |
| 101 | struct X { |
| 102 | template<typename T> struct C {}; |
| 103 | template<typename T> C(T) -> C<T>; |
| 104 | template<> C(int) -> C<int>; // expected-error {{deduction guide cannot be explicitly specialized}} |
| 105 | extern template C(float) -> C<float>; // expected-error {{expected member name or ';'}} |
| 106 | template C(char) -> C<char>; // expected-error {{expected '<' after 'template'}} |
| 107 | }; |
| 108 | } |
| 109 | |