| 1 |
// RUN: %clang_cc1 -std=c++14 -Wno-unused-value -verify %s |
| 2 |
|
| 3 |
// A glvalue of type "cv1 T1" can be cast to type "rvalue reference to |
| 4 |
// cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1" (8.5.3). |
| 5 |
struct A { }; |
| 6 |
struct B : A { }; |
| 7 |
|
| 8 |
template<typename T> T& lvalue(); |
| 9 |
template<typename T> T&& xvalue(); |
| 10 |
|
| 11 |
void test(A &a, B &b) { |
| 12 |
A &&ar0 = static_cast<A&&>(a); |
| 13 |
A &&ar1 = static_cast<A&&>(b); |
| 14 |
A &&ar2 = static_cast<A&&>(lvalue<A>()); |
| 15 |
A &&ar3 = static_cast<A&&>(lvalue<B>()); |
| 16 |
A &&ar4 = static_cast<A&&>(xvalue<A>()); |
| 17 |
A &&ar5 = static_cast<A&&>(xvalue<B>()); |
| 18 |
const A &&ar6 = static_cast<const A&&>(a); |
| 19 |
const A &&ar7 = static_cast<const A&&>(b); |
| 20 |
const A &&ar8 = static_cast<const A&&>(lvalue<A>()); |
| 21 |
const A &&ar9 = static_cast<const A&&>(lvalue<B>()); |
| 22 |
const A &&ar10 = static_cast<const A&&>(xvalue<A>()); |
| 23 |
const A &&ar11 = static_cast<const A&&>(xvalue<B>()); |
| 24 |
} |
| 25 |
|
| 26 |
namespace GH121429 { |
| 27 |
|
| 28 |
struct C : private A { // expected-note 4 {{declared private here}} |
| 29 |
C&& that(); |
| 30 |
|
| 31 |
void f() { |
| 32 |
static_cast<A&&>(*this); |
| 33 |
static_cast<const A&&>(*this); |
| 34 |
|
| 35 |
static_cast<A&&>(that()); |
| 36 |
static_cast<const A&&>(that()); |
| 37 |
} |
| 38 |
}; |
| 39 |
C c; |
| 40 |
const C cc; |
| 41 |
|
| 42 |
void f() { |
| 43 |
static_cast<A&&>(c); // expected-error {{cannot cast 'C' to its private base class 'A'}} |
| 44 |
static_cast<A&&>(c.that()); // expected-error {{cannot cast 'C' to its private base class 'A'}} |
| 45 |
|
| 46 |
static_cast<const A&&>(c); // expected-error {{cannot cast 'C' to its private base class 'const A'}} |
| 47 |
static_cast<const A&&>(c.that()); // expected-error {{cannot cast 'C' to its private base class 'const A'}} |
| 48 |
} |
| 49 |
|
| 50 |
constexpr bool g() { |
| 51 |
(A&&)c; |
| 52 |
(A&&)(C&&)c; |
| 53 |
(A&&)cc; |
| 54 |
(A&&)(const C&&)c; |
| 55 |
(const A&&)c; |
| 56 |
(const A&&)(C&&)c; |
| 57 |
(const A&&)cc; |
| 58 |
(const A&&)(const C&&)c; |
| 59 |
return true; |
| 60 |
} |
| 61 |
static_assert(g(), ""); |
| 62 |
|
| 63 |
struct D : A, B { // expected-warning {{direct base 'A' is inaccessible due to ambiguity}} |
| 64 |
D&& rv(); |
| 65 |
}; |
| 66 |
D d; |
| 67 |
|
| 68 |
void h(const D cd) { |
| 69 |
static_cast<A&&>(d); // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 70 |
static_cast<A&&>(d.rv()); // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 71 |
|
| 72 |
static_cast<const A&&>(d); // expected-error {{ambiguous conversion from derived class 'D' to base class 'const A'}} |
| 73 |
static_cast<const A&&>(d.rv()); // expected-error {{ambiguous conversion from derived class 'D' to base class 'const A'}} |
| 74 |
|
| 75 |
(A&&)d; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 76 |
(A&&)(D&&)d; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 77 |
(A&&)cd; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 78 |
(A&&)(const D&&)d; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 79 |
(const A&&)d; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 80 |
(const A&&)(D&&)d; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 81 |
(const A&&)cd; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 82 |
(const A&&)(const D&&)d; // expected-error {{ambiguous conversion from derived class 'D' to base class 'A'}} |
| 83 |
} |
| 84 |
|
| 85 |
template<class T, class U> |
| 86 |
auto s(U u = {}) -> decltype(static_cast<T&&>(u)); // expected-note 2 {{substitution failure}} |
| 87 |
|
| 88 |
int i = s<A, C>(); // expected-error {{no matching function}} |
| 89 |
int j = s<A, D>(); // expected-error {{no matching function}} |
| 90 |
|
| 91 |
} |