cxx98-compat.cpp revision b9c64d84ea3edd5e2fffb0a2e85ca1308be4f429
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wc++98-compat -verify %s 2// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s 3 4template<typename ...T> // expected-warning {{variadic templates are incompatible with C++98}} 5class Variadic1 {}; 6 7template<template<typename> class ...T> // expected-warning {{variadic templates are incompatible with C++98}} 8class Variadic2 {}; 9 10template<int ...I> // expected-warning {{variadic templates are incompatible with C++98}} 11class Variadic3 {}; 12 13int alignas(8) with_alignas; // expected-warning {{'alignas' is incompatible with C++98}} 14int with_attribute [[ ]]; // expected-warning {{attributes are incompatible with C++98}} 15 16void Literals() { 17 (void)u8"str"; // expected-warning {{unicode literals are incompatible with C++98}} 18 (void)u"str"; // expected-warning {{unicode literals are incompatible with C++98}} 19 (void)U"str"; // expected-warning {{unicode literals are incompatible with C++98}} 20 (void)u'x'; // expected-warning {{unicode literals are incompatible with C++98}} 21 (void)U'x'; // expected-warning {{unicode literals are incompatible with C++98}} 22 23 (void)u8R"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}} 24 (void)uR"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}} 25 (void)UR"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}} 26 (void)R"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}} 27 (void)LR"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}} 28} 29 30template<typename T> struct S {}; 31namespace TemplateParsing { 32 S<::S<void> > s; // expected-warning {{'<::' is treated as digraph '<:' (aka '[') followed by ':' in C++98}} 33 S< ::S<void>> t; // expected-warning {{consecutive right angle brackets are incompatible with C++98 (use '> >')}} 34} 35 36void Lambda() { 37 []{}(); // expected-warning {{lambda expressions are incompatible with C++98}} 38} 39 40int InitList() { 41 (void)new int {}; // expected-warning {{generalized initializer lists are incompatible with C++98}} \ 42 // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}} 43 (void)int{}; // expected-warning {{generalized initializer lists are incompatible with C++98}} \ 44 // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}} 45 int x { 0 }; // expected-warning {{generalized initializer lists are incompatible with C++98}} 46 return { 0 }; // expected-warning {{generalized initializer lists are incompatible with C++98}} 47} 48 49int operator""_hello(const char *); // expected-warning {{literal operators are incompatible with C++98}} 50 51enum EnumFixed : int { // expected-warning {{enumeration types with a fixed underlying type are incompatible with C++98}} 52}; 53 54enum class EnumScoped { // expected-warning {{scoped enumerations are incompatible with C++98}} 55}; 56 57void Deleted() = delete; // expected-warning {{deleted function definitions are incompatible with C++98}} 58struct Defaulted { 59 Defaulted() = default; // expected-warning {{defaulted function definitions are incompatible with C++98}} 60}; 61 62int &&RvalueReference = 0; // expected-warning {{rvalue references are incompatible with C++98}} 63struct RefQualifier { 64 void f() &; // expected-warning {{reference qualifiers on functions are incompatible with C++98}} 65}; 66 67auto f() -> int; // expected-warning {{trailing return types are incompatible with C++98}} 68 69void RangeFor() { 70 int xs[] = {1, 2, 3}; 71 for (int &a : xs) { // expected-warning {{range-based for loop is incompatible with C++98}} 72 } 73} 74 75struct InClassInit { 76 int n = 0; // expected-warning {{in-class initialization of non-static data members is incompatible with C++98}} 77}; 78 79struct OverrideControlBase { 80 virtual void f(); 81 virtual void g(); 82}; 83struct OverrideControl final : OverrideControlBase { // expected-warning {{'final' keyword is incompatible with C++98}} 84 virtual void f() override; // expected-warning {{'override' keyword is incompatible with C++98}} 85 virtual void g() final; // expected-warning {{'final' keyword is incompatible with C++98}} 86}; 87 88using AliasDecl = int; // expected-warning {{alias declarations are incompatible with C++98}} 89template<typename T> using AliasTemplate = T; // expected-warning {{alias declarations are incompatible with C++98}} 90 91inline namespace InlineNS { // expected-warning {{inline namespaces are incompatible with C++98}} 92} 93 94auto auto_deduction = 0; // expected-warning {{'auto' type specifier is incompatible with C++98}} 95int *p = new auto(0); // expected-warning {{'auto' type specifier is incompatible with C++98}} 96 97const int align_of = alignof(int); // expected-warning {{alignof expressions are incompatible with C++98}} 98char16_t c16 = 0; // expected-warning {{'char16_t' type specifier is incompatible with C++98}} 99char32_t c32 = 0; // expected-warning {{'char32_t' type specifier is incompatible with C++98}} 100constexpr int const_expr = 0; // expected-warning {{'constexpr' specifier is incompatible with C++98}} 101decltype(const_expr) decl_type = 0; // expected-warning {{'decltype' type specifier is incompatible with C++98}} 102void no_except() noexcept; // expected-warning {{noexcept specifications are incompatible with C++98}} 103bool no_except_expr = noexcept(1 + 1); // expected-warning {{noexcept expressions are incompatible with C++98}} 104void *null = nullptr; // expected-warning {{'nullptr' is incompatible with C++98}} 105static_assert(true, "!"); // expected-warning {{static_assert declarations are incompatible with C++98}} 106 107struct InhCtorBase { 108 InhCtorBase(int); 109}; 110struct InhCtorDerived : InhCtorBase { 111 using InhCtorBase::InhCtorBase; // expected-warning {{inherited constructors are incompatible with C++98}} 112}; 113 114struct FriendMember { 115 static void MemberFn(); 116 friend void FriendMember::MemberFn(); // expected-warning {{friend declaration naming a member of the declaring class is incompatible with C++98}} 117}; 118 119struct DelegCtor { 120 DelegCtor(int) : DelegCtor() {} // expected-warning {{delegating constructors are incompatible with C++98}} 121 DelegCtor(); 122}; 123 124template<int n = 0> void DefaultFuncTemplateArg(); // expected-warning {{default template arguments for a function template are incompatible with C++98}} 125 126template<typename T> int TemplateFn(T) { return 0; } 127void LocalTemplateArg() { 128 struct S {}; 129 TemplateFn(S()); // expected-warning {{local type 'S' as template argument is incompatible with C++98}} 130} 131struct {} obj_of_unnamed_type; // expected-note {{here}} 132int UnnamedTemplateArg = TemplateFn(obj_of_unnamed_type); // expected-warning {{unnamed type as template argument is incompatible with C++98}} 133 134namespace RedundantParensInAddressTemplateParam { 135 int n; 136 template<int*p> struct S {}; 137 S<(&n)> s; // expected-warning {{redundant parentheses surrounding address non-type template argument are incompatible with C++98}} 138 S<(((&n)))> t; // expected-warning {{redundant parentheses surrounding address non-type template argument are incompatible with C++98}} 139} 140 141namespace TemplateSpecOutOfScopeNs { 142 template<typename T> struct S {}; // expected-note {{here}} 143} 144template<> struct TemplateSpecOutOfScopeNs::S<char> {}; // expected-warning {{class template specialization of 'S' outside namespace 'TemplateSpecOutOfScopeNs' is incompatible with C++98}} 145 146struct Typename { 147 template<typename T> struct Inner {}; 148}; 149typename ::Typename TypenameOutsideTemplate(); // expected-warning {{use of 'typename' outside of a template is incompatible with C++98}} 150Typename::template Inner<int> TemplateOutsideTemplate(); // expected-warning {{use of 'template' keyword outside of a template is incompatible with C++98}} 151 152struct TrivialButNonPOD { 153 int f(int); 154private: 155 int k; 156}; 157void Ellipsis(int n, ...); 158void TrivialButNonPODThroughEllipsis() { 159 Ellipsis(1, TrivialButNonPOD()); // expected-warning {{passing object of trivial but non-POD type 'TrivialButNonPOD' through variadic function is incompatible with C++98}} 160} 161 162struct HasExplicitConversion { 163 explicit operator bool(); // expected-warning {{explicit conversion functions are incompatible with C++98}} 164}; 165 166struct Struct {}; 167enum Enum { enum_val = 0 }; 168struct BadFriends { 169 friend enum ::Enum; // expected-warning {{befriending enumeration type 'enum ::Enum' is incompatible with C++98}} 170 friend int; // expected-warning {{non-class friend type 'int' is incompatible with C++98}} 171 friend Struct; // expected-warning {{befriending 'Struct' without 'struct' keyword is incompatible with C++98}} 172}; 173 174int n = {}; // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}} 175 176class PrivateMember { 177 struct ImPrivate {}; 178}; 179template<typename T> typename T::ImPrivate SFINAEAccessControl(T t) { // expected-warning {{substitution failure due to access control is incompatible with C++98}} expected-note {{while substituting deduced template arguments into function template 'SFINAEAccessControl' [with T = PrivateMember]}} 180 return typename T::ImPrivate(); 181} 182int SFINAEAccessControl(...) { return 0; } 183int CheckSFINAEAccessControl = SFINAEAccessControl(PrivateMember()); 184 185template<typename T> 186struct FriendRedefinition { 187 friend void Friend() {} // expected-warning {{friend function 'Friend' would be implicitly redefined in C++98}} expected-note {{previous}} 188}; 189FriendRedefinition<int> FriendRedef1; 190FriendRedefinition<char> FriendRedef2; // expected-note {{requested here}} 191 192namespace CopyCtorIssues { 193 struct Private { 194 Private(); 195 private: 196 Private(const Private&); // expected-note {{declared private here}} 197 }; 198 struct NoViable { 199 NoViable(); 200 NoViable(NoViable&); // expected-note {{not viable}} 201 }; 202 struct Ambiguous { 203 Ambiguous(); 204 Ambiguous(const Ambiguous &, int = 0); // expected-note {{candidate}} 205 Ambiguous(const Ambiguous &, double = 0); // expected-note {{candidate}} 206 }; 207 struct Deleted { // expected-note {{here}} 208 // Copy ctor implicitly defined as deleted because Private's copy ctor is 209 // inaccessible. 210 Private p; 211 }; 212 213 const Private &a = Private(); // expected-warning {{copying variable of type 'CopyCtorIssues::Private' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}} 214 const NoViable &b = NoViable(); // expected-warning {{copying variable of type 'CopyCtorIssues::NoViable' when binding a reference to a temporary would find no viable constructor in C++98}} 215 const Ambiguous &c = Ambiguous(); // expected-warning {{copying variable of type 'CopyCtorIssues::Ambiguous' when binding a reference to a temporary would find ambiguous constructors in C++98}} 216 const Deleted &d = Deleted(); // expected-warning {{copying variable of type 'CopyCtorIssues::Deleted' when binding a reference to a temporary would invoke a deleted constructor in C++98}} 217} 218 219namespace UnionOrAnonStructMembers { 220 struct NonTrivCtor { 221 NonTrivCtor(); // expected-note 2{{user-declared constructor}} 222 }; 223 struct NonTrivCopy { 224 NonTrivCopy(const NonTrivCopy&); // expected-note 2{{user-declared copy constructor}} 225 }; 226 struct NonTrivDtor { 227 ~NonTrivDtor(); // expected-note 2{{user-declared destructor}} 228 }; 229 union BadUnion { 230 NonTrivCtor ntc; // expected-warning {{union member 'ntc' with a non-trivial constructor is incompatible with C++98}} 231 NonTrivCopy ntcp; // expected-warning {{union member 'ntcp' with a non-trivial copy constructor is incompatible with C++98}} 232 NonTrivDtor ntd; // expected-warning {{union member 'ntd' with a non-trivial destructor is incompatible with C++98}} 233 }; 234 struct Wrap { 235 struct { 236 NonTrivCtor ntc; // expected-warning {{anonymous struct member 'ntc' with a non-trivial constructor is incompatible with C++98}} 237 NonTrivCopy ntcp; // expected-warning {{anonymous struct member 'ntcp' with a non-trivial copy constructor is incompatible with C++98}} 238 NonTrivDtor ntd; // expected-warning {{anonymous struct member 'ntd' with a non-trivial destructor is incompatible with C++98}} 239 }; 240 }; 241 union WithStaticDataMember { 242 static constexpr double d = 0.0; // expected-warning {{static data member 'd' in union is incompatible with C++98}} expected-warning {{'constexpr' specifier is incompatible with C++98}} 243 static const int n = 0; // expected-warning {{static data member 'n' in union is incompatible with C++98}} 244 static int k; // expected-warning {{static data member 'k' in union is incompatible with C++98}} 245 }; 246} 247 248int EnumNNS = Enum::enum_val; // expected-warning {{enumeration type in nested name specifier is incompatible with C++98}} 249template<typename T> void EnumNNSFn() { 250 int k = T::enum_val; // expected-warning {{enumeration type in nested name specifier is incompatible with C++98}} 251}; 252template void EnumNNSFn<Enum>(); // expected-note {{in instantiation}} 253 254void JumpDiagnostics(int n) { 255 goto DirectJump; // expected-warning {{goto would jump into protected scope in C++98}} 256 TrivialButNonPOD tnp1; // expected-note {{jump bypasses initialization of non-POD variable}} 257 258DirectJump: 259 void *Table[] = {&&DirectJump, &&Later}; 260 goto *Table[n]; // expected-warning {{indirect goto might cross protected scopes in C++98}} 261 262 TrivialButNonPOD tnp2; // expected-note {{jump bypasses initialization of non-POD variable}} 263Later: // expected-note {{possible target of indirect goto}} 264 switch (n) { 265 TrivialButNonPOD tnp3; // expected-note {{jump bypasses initialization of non-POD variable}} 266 default: // expected-warning {{switch case would be in a protected scope in C++98}} 267 return; 268 } 269} 270