| 1 |
// RUN: %clang_cc1 -verify -std=c++20 %s -fsyntax-only |
| 2 |
|
| 3 |
namespace std { |
| 4 |
template <class T> struct remove_reference { typedef T type; }; |
| 5 |
template <class T> struct remove_reference<T&> { typedef T type; }; |
| 6 |
template <class T> struct remove_reference<T&&> { typedef T type; }; |
| 7 |
|
| 8 |
template <class T> typename remove_reference<T>::type &&move(T &&t); |
| 9 |
} // namespace std |
| 10 |
|
| 11 |
// dcl.init 16.6.2.2 |
| 12 |
struct A { |
| 13 |
int a; |
| 14 |
int&& r; |
| 15 |
}; |
| 16 |
|
| 17 |
int f(); |
| 18 |
int n = 10; |
| 19 |
|
| 20 |
A a1{1, f()}; // OK, lifetime is extended for direct-list-initialization |
| 21 |
// well-formed, but dangling reference |
| 22 |
A a2(1, f()); // expected-warning {{temporary whose address is used as value}} |
| 23 |
// well-formed, but dangling reference |
| 24 |
A a4(1.0, 1); // expected-warning {{temporary whose address is used as value}} |
| 25 |
A a5(1.0, std::move(n)); // OK |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
struct B { |
| 30 |
const int& r; |
| 31 |
}; |
| 32 |
B test(int local) { |
| 33 |
return B(1); // expected-warning {{returning address}} |
| 34 |
return B(local); // expected-warning {{address of stack memory}} |
| 35 |
} |
| 36 |
|
| 37 |
void f(B b); |
| 38 |
void test2(int local) { |
| 39 |
// No diagnostic on the following cases where both the aggregate object and |
| 40 |
// temporary end at the end of the full expression. |
| 41 |
f(B(1)); |
| 42 |
f(B(local)); |
| 43 |
} |
| 44 |
|
| 45 |
// Test nested struct. |
| 46 |
struct C { |
| 47 |
B b; |
| 48 |
}; |
| 49 |
|
| 50 |
struct D { |
| 51 |
C c; |
| 52 |
}; |
| 53 |
|
| 54 |
C c1(B( |
| 55 |
1 // expected-warning {{temporary whose address is used as value}} |
| 56 |
)); |
| 57 |
D d1(C(B( |
| 58 |
1 // expected-warning {{temporary whose address is used as value}} |
| 59 |
))); |