251 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wno-c99-designator %s2// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -Wno-c99-designator %s3 4// PR52905int const f0();6void f0_test() {7 decltype(0, f0()) i = 0;8 i = 0;9}10 11struct A { int a[1]; A() { } };12typedef A const AC;13int &f1(int*);14float &f2(int const*);15 16void test_f2() {17 float &fr = f2(AC().a);18}19 20template <class T>21struct Future {22 explicit Future(T v);23 24 template <class F>25 auto call(F&& fn) -> decltype(fn(T())) {26 return fn(T());27 }28 29 template <class B, class F>30 auto then(F&& fn) -> decltype(call(fn))31 {32 return fn(T());33 }34};35 36void rdar16527205() {37 Future<int> f1(42);38 f1.call([](int){ return Future<float>(0); });39}40 41namespace pr10154 {42 class A{43 A(decltype(nullptr) param);44 };45}46 47template<typename T> struct S {};48template<typename T> auto f(T t) -> decltype(S<int>(t)) {49 using U = decltype(S<int>(t));50 using U = S<int>;51 return S<int>(t);52}53 54struct B {55 B(decltype(undeclared)); // expected-error {{undeclared identifier}}56};57struct C {58 C(decltype(undeclared; // expected-error {{undeclared identifier}} \59 // expected-error {{expected ')'}} expected-note {{to match this '('}}60};61 62namespace PR16529 {63 struct U {};64 template <typename T> struct S {65 static decltype(T{}, U{}) &f();66 };67 U &r = S<int>::f();68}69 70namespace PR18876 {71 struct A { ~A() = delete; }; // expected-note +{{here}}72 A f();73 decltype(f()) *a; // ok, function call74 decltype(A()) *b; // expected-error {{attempt to use a deleted function}}75 decltype(0, f()) *c; // ok, function call on RHS of comma76 decltype(0, A()) *d; // expected-error {{attempt to use a deleted function}}77 decltype(f(), 0) *e; // expected-error {{attempt to use a deleted function}}78}79 80namespace D5789 {81 struct P1 { char x[6]; } g1 = { "foo" };82 struct LP1 { struct P1 p1; };83 84 template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {} // expected-note {{previous definition is here}}85 86 template<class T>87 void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'r' }))) {} // okay88 89 template<class T>90 void foo(decltype(T(LP1{ .p1 = { "foo" }, .p1.x[1] = 'x'}))) {} // okay91 92 // expected-error@+1 {{redefinition of 'foo'}}93 template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {}94}95 96namespace GH58674 {97 struct Foo {98 float value_;99 struct nested {100 float value_;101 };102 };103 104 template <typename T>105 struct TemplateFoo {106 float value_;107 };108 109 float bar;110 111 template <typename T>112 struct Animal{};113 114 template <typename T>115 class Cat : Animal<T> {116 using okay = decltype(Foo::value_);117 using also_okay = decltype(bar);118 using okay2 = decltype(Foo::nested::value_);119 using okay3 = decltype(TemplateFoo<T>::value_);120 public:121 void meow() {122 using okay = decltype(Foo::value_);123 using also_okay = decltype(bar);124 using okay2 = decltype(Foo::nested::value_);125 using okay3 = decltype(TemplateFoo<T>::value_);126 }127 };128 129 void baz() {130 Cat<void>{}.meow();131 }132}133 134namespace GH97646 {135 template<bool B>136 void f() {137 decltype(B) x = false;138 !x;139 }140}141 142namespace GH99873 {143struct B {144 int x;145};146 147template<typename T>148struct A {149 template<typename U>150 constexpr int f() const {151 return 1;152 }153 154 template<>155 constexpr int f<int>() const {156 return decltype(B::x)();157 }158};159 160 161 162// This shouldn't crash.163static_assert(A<int>().f<int>() == 0, "");164// The result should not be dependent.165static_assert(A<int>().f<int>() != 0, ""); // expected-error {{static assertion failed due to requirement 'GH99873::A<int>().f<int>() != 0'}}166 // expected-note@-1 {{expression evaluates to '0 != 0'}}167}168 169 170#if __cplusplus >= 201703L171namespace GH160497 {172 173template <class> struct S {174 template <class>175 inline static auto mem =176 [] { static_assert(false); // expected-error {{static assertion failed}} \177 // expected-note {{while substituting into a lambda expression here}}178 return 42;179 }();180};181 182using T = decltype(S<void>::mem<void>);183 // expected-note@-1 {{in instantiation of static data member 'GH160497::S<void>::mem<void>' requested here}}184 185 186template <class> struct S2 {187 template <class>188 inline static auto* mem =189 [] { static_assert(false); // expected-error {{static assertion failed}} \190 // expected-note {{while substituting into a lambda expression here}}191 return static_cast<int*>(nullptr);192 }();193};194 195using T2 = decltype(S2<void>::mem<void>);196//expected-note@-1 {{in instantiation of static data member 'GH160497::S2<void>::mem<void>' requested here}}197 198template <class> struct S3 {199 template <class>200 inline static int mem = // Check we don't instantiate when the type is not deduced.201 [] { static_assert(false);202 return 42;203 }();204};205 206using T = decltype(S3<void>::mem<void>);207}208 209namespace N1 {210 211template<class>212struct S {213 template<class>214 inline static auto mem = 42;215};216 217using T = decltype(S<void>::mem<void>);218 219T y = 42;220 221}222 223namespace GH161196 {224 225template <typename> struct A {226 static constexpr int digits = 0;227};228 229template <typename> struct B {230 template <int, typename MaskInt = int, int = A<MaskInt>::digits>231 static constexpr auto XBitMask = 0;232};233 234struct C {235 using ReferenceHost = B<int>;236 template <int> static decltype(ReferenceHost::XBitMask<0>) XBitMask;237};238 239void test() { (void)C::XBitMask<0>; }240 241}242#endif243 244template<typename>245class conditional {246};247 248// FIXME: The diagnostics here are produced twice.249void foo(conditional<decltype((1),int>) { // expected-note 2 {{to match this '('}} expected-error {{expected ')'}} expected-note 2{{to match this '<'}}250} // expected-error {{expected function body after function declarator}} expected-error 2 {{expected '>'}} expected-error {{expected ')'}}251