317 lines · cpp
1// RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98-11,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors2// RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx98-11,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors3// RUN: %clang_cc1 -std=c++14 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors4// RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors5// RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors6// RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors7// RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors8 9namespace std {10struct type_info;11} // namespace std12 13namespace cwg1900 { // cwg1900: 2.714// See the test for CWG1477 for detailed analysis15namespace N {16struct A {17 friend int f();18};19}20int N::f() { return 0; }21int N::g() { return 0; }22// expected-error@-1 {{out-of-line definition of 'g' does not match any declaration in namespace 'cwg1900::N'}}23} // namespace cwg190024 25namespace cwg1902 { // cwg1902: 3.726 struct A {};27 struct B {28 B(A); // #cwg1902-B-A29 B() = delete; // #cwg1902-B-ctor30 // cxx98-error@-1 {{deleted function definitions are a C++11 extension}}31 B(const B&) = delete; // #cwg1902-B-copy-ctor32 // cxx98-error@-1 {{deleted function definitions are a C++11 extension}}33 operator A();34 };35 36 extern B b1;37 B b2(b1);38 // expected-error@-1 {{call to deleted constructor of 'B'}}39 // expected-note@#cwg1902-B-copy-ctor {{'B' has been explicitly marked deleted here}}40 41#if __cplusplus >= 201103L42 // This is ambiguous, even though calling the B(const B&) constructor would43 // both directly and indirectly call a deleted function.44 B b({});45 // since-cxx11-error@-1 {{call to constructor of 'B' is ambiguous}}46 // since-cxx11-note@#cwg1902-B-A {{candidate constructor}}47 // since-cxx11-note@#cwg1902-B-copy-ctor {{candidate constructor has been explicitly deleted}}48#endif49} // namespace cwg190250 51namespace cwg1903 { // cwg1903: 2.752 namespace A {53 struct a {};54 int a;55 namespace B {56 int b;57 }58 using namespace B;59 namespace {60 int c;61 }62 namespace D {63 int d;64 }65 using D::d;66 }67 namespace X {68 using A::a;69 using A::b;70 using A::c;71 using A::d;72 struct a *p;73 }74} // namespace cwg190375 76namespace cwg1909 { // cwg1909: 3.777 struct A {78 template<typename T> struct A {};79 // expected-error@-1 {{member 'A' has the same name as its class}}80 };81 struct B {82 template<typename T> void B() {}83 // expected-error@-1 {{constructor cannot have a return type}}84 };85 struct C {86 template<typename T> static int C;87 // expected-error@-1 {{member 'C' has the same name as its class}}88 // cxx98-11-error@-2 {{variable templates are a C++14 extension}}89 };90 struct D {91 template<typename T> using D = int;92 // cxx98-error@-1 {{alias declarations are a C++11 extension}}93 // expected-error@-2 {{member 'D' has the same name as its class}}94 };95} // namespace cwg190996 97namespace cwg1918 { // cwg1918: no98template<typename T> struct A {99 class B {100 class C {};101 };102};103class X {104 static int x;105 // FIXME: this is ill-formed, because A<T>::B::C does not end with a simple-template-id106 template <typename T>107 friend class A<T>::B::C;108 // expected-warning@-1 {{dependent nested name specifier 'A<T>::B' for friend class declaration is not supported; turning off access control for 'X'}}109};110template<> struct A<int> {111 typedef struct Q B;112};113struct Q {114 class C {115 // FIXME: 'f' is not a friend, so 'X::x' is not accessible116 int f() { return X::x; }117 };118};119} // namespace cwg1918120 121namespace cwg1940 { // cwg1940: 3.5122#if __cplusplus >= 201103L123static union {124 static_assert(true, ""); // ok125 static_assert(false, "");126 // since-cxx11-error@-1 {{static assertion failed}}127 int not_empty;128};129#endif130} // namespace cwg1918131 132namespace cwg1941 { // cwg1941: 3.9133#if __cplusplus >= 201402L134template<typename X>135struct base {136 template<typename T>137 base(T a, T b, decltype(void(*T()), 0) = 0) {138 while (a != b) (void)*a++;139 }140 141 template<typename T>142 base(T a, X x, decltype(void(T(0) * 1), 0) = 0) {143 for (T n = 0; n != a; ++n) (void)X(x);144 }145};146 147struct derived : base<int> {148 using base::base;149};150 151struct iter {152 iter operator++(int);153 int operator*();154 friend bool operator!=(iter, iter);155} it, end;156 157derived d1(it, end);158derived d2(42, 9);159#endif160} // namespace cwg1941161 162namespace cwg1945 { // cwg1945: no163template<typename T> struct A {164 class B {165 class C {};166 };167};168class X {169 static int x;170 // FIXME: this is ill-formed, because A<T>::B::C does not end with a simple-template-id171 template <typename T>172 friend class A<T>::B::C;173 // expected-warning@-1 {{dependent nested name specifier 'A<T>::B' for friend class declaration is not supported; turning off access control for 'X'}}174};175} // namespace cwg1945176 177namespace cwg1947 { // cwg1947: 3.5178#if __cplusplus >= 201402L179unsigned o = 0'01; // ok180unsigned b = 0b'01;181// since-cxx14-error@-1 {{invalid digit 'b' in octal constant}}182unsigned x = 0x'01;183// since-cxx14-error@-1 {{invalid suffix 'x'01' on integer constant}}184#endif185} // namespace cwg1947186 187#if __cplusplus >= 201103L188// cwg1948: 3.5189// FIXME: This diagnostic could be improved.190void *operator new(__SIZE_TYPE__) noexcept { return nullptr; }191// since-cxx11-error@-1 {{exception specification in declaration does not match previous declaration}}192#endif193 194namespace cwg1959 { // cwg1959: 3.9195#if __cplusplus >= 201103L196 struct b;197 struct c;198 struct a {199 a() = default;200 a(const a &) = delete; // #cwg1959-copy-ctor201 a(const b &) = delete; // not inherited202 a(c &&) = delete; // #cwg1959-move-ctor203 template<typename T> a(T) = delete; // #cwg1959-temp-ctor204 };205 206 struct b : a { // #cwg1959-b207 using a::a; // #cwg1959-using-a208 };209 210 a x;211 // FIXME: As a resolution to an open DR against P0136R0, we disallow212 // use of inherited constructors to construct from a single argument213 // where the base class is reference-related to the argument type.214 b y = x;215 // since-cxx11-error@-1 {{no viable conversion from 'a' to 'b'}}216 // since-cxx11-note@#cwg1959-move-ctor {{candidate inherited constructor not viable: no known conversion from 'a' to 'c &&' for 1st argument}}217 // since-cxx11-note@#cwg1959-using-a {{constructor from base class 'a' inherited here}}218 // since-cxx11-note@#cwg1959-b {{candidate constructor (the implicit copy constructor) not viable: cannot bind base class object of type 'a' to derived class reference 'const b &' for 1st argument}}219 // since-cxx11-note@#cwg1959-temp-ctor {{candidate template ignored: instantiation would take its own class type by value}}220 // since-cxx11-note@#cwg1959-using-a {{constructor from base class 'a' inherited here}}221 b z = z;222 // since-cxx11-error@-1 {{call to implicitly-deleted copy constructor of 'b'}}223 // since-cxx11-note@#cwg1959-b {{copy constructor of 'b' is implicitly deleted because base class 'a' has a deleted copy constructor}}224 // since-cxx11-note@#cwg1959-copy-ctor {{'a' has been explicitly marked deleted here}}225 226 struct c : a {227 using a::a;228 c(const c &);229 };230 // FIXME: As a resolution to an open DR against P0136R0, we disallow231 // use of inherited constructors to construct from a single argument232 // where the base class is reference-related to the argument type.233 c q(static_cast<c&&>(q));234#endif235} // namespace cwg1959236 237namespace cwg1960 { // cwg1960: no238struct A {239void f() {}240protected:241void g() {}242};243 244struct B: A {245private:246using A::f;247using A::g;248};249 250struct C : B {251// FIXME: both declarations are ill-formed, because A::f and A::g252// are not accessible.253using A::f;254using A::g;255};256} // namespace cwg1960257 258namespace cwg1966 { // cwg1966: 11259#if __cplusplus >= 201103L260 struct A {261 enum E : int {1};262 // since-cxx11-error@-1 {{expected identifier}} (not bit-field)263 };264 auto *p1 = new enum E : int;265 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration}}266 auto *p2 = new enum F : int {};267 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration}}268 auto *p3 = true ? new enum G : int {};269 // since-cxx11-error@-1 {{ISO C++ forbids forward references to 'enum' types}}270 // since-cxx11-error@-2 {{allocation of incomplete type 'enum G'}}271 // since-cxx11-note@-3 {{forward declaration of 'cwg1966::G'}}272 auto h() -> enum E : int {};273 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration}}274 275 enum X : enum Y : int {} {};276 // since-cxx11-error@-1 {{'cwg1966::Y' cannot be defined in a type specifier}}277 struct Q {278 // FIXME: can we emit something nicer than that?279 enum X : enum Y : int {} {};280 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}}281 // since-cxx11-error@-2 {{non-integral type 'enum Y' is an invalid underlying type}}282 // since-cxx11-error@-3 {{anonymous bit-field cannot have a default member initializer}}283 };284#endif285} // namespace cwg1966286 287namespace cwg1968 { // cwg1968: no288#if __cplusplus >= 201103L289 // FIXME: According to CWG1968, both of these should be considered290 // non-constant.291 static_assert(&typeid(int) == &typeid(int), "");292 293 constexpr const std::type_info *f() { return &typeid(int); }294 static_assert(f() == f(), "");295#endif296} // namespace cwg1968297 298namespace cwg1991 { // cwg1991: 3.9299#if __cplusplus >= 201103L300 struct A {301 A(int, int) = delete;302 };303 304 struct B : A {305 using A::A;306 B(int, int, int = 0);307 };308 309 // FIXME: As a resolution to an open DR against P0136R1, we treat derived310 // class constructors as better than base class constructors in the presence311 // of ambiguity.312 B b(0, 0); // ok, calls B constructor313#endif314} // namespace cwg1991315 316// cwg1994: dup 529317