[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / decltype.cpp
blob76d6a041d6dcc68d1080e07c9c7ba94c9803149f
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wno-c99-designator %s
3 // PR5290
4 int const f0();
5 void f0_test() {
6 decltype(0, f0()) i = 0;
7 i = 0;
10 struct A { int a[1]; A() { } };
11 typedef A const AC;
12 int &f1(int*);
13 float &f2(int const*);
15 void test_f2() {
16 float &fr = f2(AC().a);
19 template <class T>
20 struct Future {
21 explicit Future(T v);
23 template <class F>
24 auto call(F&& fn) -> decltype(fn(T())) {
25 return fn(T());
28 template <class B, class F>
29 auto then(F&& fn) -> decltype(call(fn))
31 return fn(T());
35 void rdar16527205() {
36 Future<int> f1(42);
37 f1.call([](int){ return Future<float>(0); });
40 namespace pr10154 {
41 class A{
42 A(decltype(nullptr) param);
46 template<typename T> struct S {};
47 template<typename T> auto f(T t) -> decltype(S<int>(t)) {
48 using U = decltype(S<int>(t));
49 using U = S<int>;
50 return S<int>(t);
53 struct B {
54 B(decltype(undeclared)); // expected-error {{undeclared identifier}}
56 struct C {
57 C(decltype(undeclared; // expected-error {{undeclared identifier}} \
58 // expected-error {{expected ')'}} expected-note {{to match this '('}}
61 namespace PR16529 {
62 struct U {};
63 template <typename T> struct S {
64 static decltype(T{}, U{}) &f();
66 U &r = S<int>::f();
69 namespace PR18876 {
70 struct A { ~A() = delete; }; // expected-note +{{here}}
71 A f();
72 decltype(f()) *a; // ok, function call
73 decltype(A()) *b; // expected-error {{attempt to use a deleted function}}
74 decltype(0, f()) *c; // ok, function call on RHS of comma
75 decltype(0, A()) *d; // expected-error {{attempt to use a deleted function}}
76 decltype(f(), 0) *e; // expected-error {{attempt to use a deleted function}}
79 namespace D5789 {
80 struct P1 { char x[6]; } g1 = { "foo" };
81 struct LP1 { struct P1 p1; };
83 // expected-warning@+3 {{initializer partially overrides}}
84 // expected-note@+2 {{previous initialization}}
85 // expected-note@+1 {{previous definition}}
86 template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {}
88 // expected-warning@+3 {{initializer partially overrides}}
89 // expected-note@+2 {{previous initialization}}
90 template<class T>
91 void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'r' }))) {} // okay
93 // expected-warning@+3 {{initializer partially overrides}}
94 // expected-note@+2 {{previous initialization}}
95 template<class T>
96 void foo(decltype(T(LP1{ .p1 = { "foo" }, .p1.x[1] = 'x'}))) {} // okay
98 // expected-warning@+3 {{initializer partially overrides}}
99 // expected-note@+2 {{previous initialization}}
100 // expected-error@+1 {{redefinition of 'foo'}}
101 template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {}
104 namespace GH58674 {
105 struct Foo {
106 float value_;
107 struct nested {
108 float value_;
112 template <typename T>
113 struct TemplateFoo {
114 float value_;
117 float bar;
119 template <typename T>
120 struct Animal{};
122 template <typename T>
123 class Cat : Animal<T> {
124 using okay = decltype(Foo::value_);
125 using also_okay = decltype(bar);
126 using okay2 = decltype(Foo::nested::value_);
127 using okay3 = decltype(TemplateFoo<T>::value_);
128 public:
129 void meow() {
130 using okay = decltype(Foo::value_);
131 using also_okay = decltype(bar);
132 using okay2 = decltype(Foo::nested::value_);
133 using okay3 = decltype(TemplateFoo<T>::value_);
137 void baz() {
138 Cat<void>{}.meow();
142 namespace GH97646 {
143 template<bool B>
144 void f() {
145 decltype(B) x = false;
150 namespace GH99873 {
151 struct B {
152 int x;
155 template<typename T>
156 struct A {
157 template<typename U>
158 constexpr int f() const {
159 return 1;
162 template<>
163 constexpr int f<int>() const {
164 return decltype(B::x)();
168 // This shouldn't crash.
169 static_assert(A<int>().f<int>() == 0, "");
170 // The result should not be dependent.
171 static_assert(A<int>().f<int>() != 0, ""); // expected-error {{static assertion failed due to requirement 'GH99873::A<int>().f<int>() != 0'}}
172 // expected-note@-1 {{expression evaluates to '0 != 0'}}
175 template<typename>
176 class conditional {
179 // FIXME: The diagnostics here are produced twice.
180 void foo(conditional<decltype((1),int>) { // expected-note 2 {{to match this '('}} expected-error {{expected ')'}} expected-note 2{{to match this '<'}}
181 } // expected-error {{expected function body after function declarator}} expected-error 2 {{expected '>'}} expected-error {{expected ')'}}