| 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
| 3 | struct X0 { |
| 4 | X0(); |
| 5 | X0(int); |
| 6 | X0 f1(); |
| 7 | X0 f2(); |
| 8 | typedef int A; |
| 9 | typedef X0 B; |
| 10 | }; |
| 11 | |
| 12 | template<typename T> |
| 13 | struct X1 : X0 { |
| 14 | X1(); |
| 15 | X1<T>(int); |
| 16 | (X1<T>)(float); |
| 17 | X1 f2(); |
| 18 | X1 f2(int); |
| 19 | X1 f2(float); |
| 20 | X1 f2(double); |
| 21 | X1 f2(short); |
| 22 | X1 f2(long); |
| 23 | }; |
| 24 | |
| 25 | // Error recovery: out-of-line constructors whose names have template arguments. |
| 26 | template<typename T> X1<T>::X1<T>(int) { } // expected-error{{out-of-line constructor for 'X1' cannot have template arguments}} |
| 27 | template<typename T> (X1<T>::X1<T>)(float) { } // expected-error{{out-of-line constructor for 'X1' cannot have template arguments}} |
| 28 | |
| 29 | // Error recovery: out-of-line constructor names intended to be types |
| 30 | X0::X0 X0::f1() { return X0(); } // expected-error{{qualified reference to 'X0' is a constructor name rather than a type in this context}} |
| 31 | |
| 32 | struct X0::X0 X0::f2() { return X0(); } |
| 33 | |
| 34 | template<typename T> X1<T>::X1<T> X1<T>::f2() { } // expected-error{{missing 'typename'}} |
| 35 | template<typename T> X1<T>::X1<T> (X1<T>::f2)(int) { } // expected-error{{missing 'typename'}} |
| 36 | template<typename T> struct X1<T>::X1<T> (X1<T>::f2)(float) { } |
| 37 | template<typename T> struct X1<T>::X1 (X1<T>::f2)(double) { } |
| 38 | template<typename T> typename X1<T>::template X1<T> X1<T>::f2(short) { } // expected-warning {{qualified reference to 'X1' is a constructor name rather than a template name in this context}} |
| 39 | template<typename T> typename X1<T>::template X1<T> (X1<T>::f2)(long) { } // expected-warning {{qualified reference to 'X1' is a constructor name rather than a template name in this context}} |
| 40 | |
| 41 | void x1test(X1<int> x1i) { |
| 42 | x1i.f2(); |
| 43 | x1i.f2(0); |
| 44 | x1i.f2(0.f); |
| 45 | x1i.f2(0.); |
| 46 | } |
| 47 | |
| 48 | void other_contexts() { |
| 49 | X0::X0 x0; // expected-error{{qualified reference to 'X0' is a constructor name rather than a type in this context}} |
| 50 | X1<int>::X1 x1a; // expected-error{{qualified reference to 'X1' is a constructor name rather than a type in this context}} |
| 51 | X1<int>::X1<float> x1b; // expected-error{{qualified reference to 'X1' is a constructor name rather than a template name in this context}} |
| 52 | |
| 53 | X0::B ok1; |
| 54 | X0::X0::A ok2; |
| 55 | X0::X0::X0 x0b; // expected-error{{qualified reference to 'X0' is a constructor name rather than a type in this context}} |
| 56 | X1<int>::X0 ok3; |
| 57 | X1<int>::X0::X0 x0c; // expected-error{{qualified reference to 'X0' is a constructor name rather than a type in this context}} |
| 58 | X1<int>::X1<float>::X0 ok4; |
| 59 | |
| 60 | { |
| 61 | typename X0::X0 tn1; // expected-warning{{qualified reference to 'X0' is a constructor name rather than a type in this context}} expected-warning 0-1{{typename}} |
| 62 | typename X1<int>::X1<float> tn2; // expected-warning{{qualified reference to 'X1' is a constructor name rather than a template name in this context}} expected-warning 0-1{{typename}} |
| 63 | typename X0::B ok1; // expected-warning 0-1{{typename}} |
| 64 | typename X1<int>::X0 ok2; // expected-warning 0-1{{typename}} |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | struct X0::X0 tag1; |
| 69 | struct X1<int>::X1 tag2; |
| 70 | struct X1<int>::X1<int> tag3; |
| 71 | } |
| 72 | |
| 73 | int a; |
| 74 | { |
| 75 | X0::X0(a); // expected-error{{qualified reference to 'X0' is a constructor name rather than a type in this context}} |
| 76 | // expected-warning@-1 {{redundant parentheses around declaration of variable named 'a'}} expected-note@-1 2{{}} |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | template<typename T> void in_instantiation_x0() { |
| 81 | typename T::X0 x0; // expected-warning{{qualified reference to 'X0' is a constructor name rather than a type in this context}} |
| 82 | typename T::A a; |
| 83 | typename T::B b; |
| 84 | } |
| 85 | template void in_instantiation_x0<X0>(); // expected-note {{instantiation of}} |
| 86 | |
| 87 | template<typename T> void in_instantiation_x1() { |
| 88 | typename T::X1 x1; // expected-warning{{qualified reference to 'X1' is a constructor name rather than a type in this context}} |
| 89 | typename T::template X1<int> x1i; // expected-warning{{qualified reference to 'X1' is a constructor name rather than a template name in this context}} |
| 90 | typename T::X0 x0; |
| 91 | } |
| 92 | template void in_instantiation_x1<X1<int> >(); // expected-note {{instantiation of}} |
| 93 | |
| 94 | namespace sfinae { |
| 95 | template<typename T> void f(typename T::X0 *) = delete; // expected-warning 0-1{{extension}} |
| 96 | template<typename T> void f(...); |
| 97 | void g() { f<X0>(0); } |
| 98 | } |
| 99 | |
| 100 | namespace versus_injected_class_name { |
| 101 | template <typename T> struct A : T::B { |
| 102 | struct T::B *p; |
| 103 | typename T::B::type a; |
| 104 | A() : T::B() {} |
| 105 | |
| 106 | typename T::B b; // expected-warning {{qualified reference to 'B' is a constructor name rather than a type in this context}} |
| 107 | }; |
| 108 | struct B { |
| 109 | typedef int type; |
| 110 | }; |
| 111 | template struct A<B>; // expected-note {{in instantiation of}} |
| 112 | } |
| 113 | |
| 114 | // We have a special case for lookup within using-declarations that are |
| 115 | // member-declarations: foo::bar::baz::baz always names baz's constructor |
| 116 | // in such a context, even if looking up 'baz' within foo::bar::baz would |
| 117 | // not find the injected-class-name. Likewise foo::bar::baz<T>::baz also |
| 118 | // names the constructor. |
| 119 | namespace InhCtor { |
| 120 | struct A { |
| 121 | A(int); |
| 122 | protected: |
| 123 | int T(); |
| 124 | }; |
| 125 | typedef A T; |
| 126 | struct B : A { |
| 127 | // This is a using-declaration for 'int A::T()' in C++98, but is an |
| 128 | // inheriting constructor declaration in C++11. |
| 129 | using InhCtor::T::T; |
| 130 | }; |
| 131 | #if __cplusplus < 201103L |
| 132 | B b(123); // expected-error {{no matching constructor}} |
| 133 | // expected-note@-7 2{{candidate constructor}} |
| 134 | int n = b.T(); // ok, accessible |
| 135 | #else |
| 136 | B b(123); // ok, inheriting constructor |
| 137 | int n = b.T(); // expected-error {{'T' is a protected member of 'InhCtor::A'}} |
| 138 | // expected-note@-15 {{declared protected here}} |
| 139 | |
| 140 | // FIXME: EDG and GCC reject this too, but it's not clear why it would be |
| 141 | // ill-formed. |
| 142 | template<typename T> |
| 143 | struct S : T { |
| 144 | struct U : S { // expected-note 6{{candidate}} |
| 145 | using S::S; |
| 146 | }; |
| 147 | using T::T; |
| 148 | }; |
| 149 | S<A>::U ua(0); // expected-error {{no match}} |
| 150 | S<B>::U ub(0); // expected-error {{no match}} |
| 151 | |
| 152 | template<typename T> |
| 153 | struct X : T { |
| 154 | using T::Z::U::U; |
| 155 | }; |
| 156 | template<typename T> |
| 157 | struct X2 : T { |
| 158 | using T::Z::template V<int>::V; |
| 159 | }; |
| 160 | struct Y { |
| 161 | struct Z { |
| 162 | typedef Y U; |
| 163 | template<typename T> using V = Y; |
| 164 | }; |
| 165 | Y(int); |
| 166 | }; |
| 167 | X<Y> xy(0); |
| 168 | |
| 169 | namespace Repeat { |
| 170 | struct A { |
| 171 | struct T { |
| 172 | T(int); |
| 173 | }; |
| 174 | }; |
| 175 | struct Z : A { |
| 176 | using A::A::A; |
| 177 | }; |
| 178 | template<typename T> |
| 179 | struct ZT : T::T { |
| 180 | using T::T::T; |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | namespace NS { |
| 185 | struct NS {}; |
| 186 | } |
| 187 | struct DerivedFromNS : NS::NS { |
| 188 | // No special case unless the NNS names a class. |
| 189 | using InhCtor::NS::NS; // expected-error {{using declaration in class refers into 'InhCtor::NS::', which is not a class}} |
| 190 | |
| 191 | }; |
| 192 | |
| 193 | // FIXME: Consider reusing the same diagnostic between dependent and non-dependent contexts |
| 194 | typedef int I; |
| 195 | struct UsingInt { |
| 196 | using I::I; // expected-error {{'InhCtor::I' (aka 'int') is not a class, namespace, or enumeration}} |
| 197 | }; |
| 198 | template<typename T> struct UsingIntTemplate { |
| 199 | using T::T; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} |
| 200 | }; |
| 201 | UsingIntTemplate<int> uit; // expected-note {{here}} |
| 202 | |
| 203 | // This case is odd: we don't name the constructor of a dependent base as |
| 204 | // Base::Base, but we still happen to have enough information to identify |
| 205 | // when parsing the template that we're inheriting constructors. |
| 206 | // |
| 207 | // FIXME: Once CWG 2070 is resolved, check whether this case should be |
| 208 | // accepted or not. |
| 209 | namespace DependentCtorName { |
| 210 | template <typename T> struct B { B(int); }; |
| 211 | template <typename T> struct A : B<T> { |
| 212 | using X = B<T>; |
| 213 | using X::B; |
| 214 | }; |
| 215 | A<int> ab = 0; |
| 216 | } |
| 217 | #endif |
| 218 | } |
| 219 | |