1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++98, c++03, c++11, c++14
10 // UNSUPPORTED: dylib-has-no-bad_any_cast
11
12 // <any>
13
14 // template <class ValueType>
15 // ValueType any_cast(any &&);
16
17 // Try and use the rvalue any_cast to cast to an lvalue reference
18
19 #include <any>
20
21 struct TestType {};
22 using std::any;
23 using std::any_cast;
24
test_const_lvalue_cast_request_non_const_lvalue()25 void test_const_lvalue_cast_request_non_const_lvalue()
26 {
27 const any a;
28 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}}
29 // expected-error@any:* {{drops 'const' qualifier}}
30 any_cast<TestType &>(a); // expected-note {{requested here}}
31
32 const any a2(42);
33 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}}
34 // expected-error@any:* {{drops 'const' qualifier}}
35 any_cast<int&>(a2); // expected-note {{requested here}}
36 }
37
test_lvalue_any_cast_request_rvalue()38 void test_lvalue_any_cast_request_rvalue()
39 {
40 any a;
41 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an lvalue reference or a CopyConstructible type"}}
42 any_cast<TestType &&>(a); // expected-note {{requested here}}
43
44 any a2(42);
45 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an lvalue reference or a CopyConstructible type"}}
46 any_cast<int&&>(a2); // expected-note {{requested here}}
47 }
48
test_rvalue_any_cast_request_lvalue()49 void test_rvalue_any_cast_request_lvalue()
50 {
51 any a;
52 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an rvalue reference or a CopyConstructible type"}}
53 // expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}}
54 any_cast<TestType &>(std::move(a)); // expected-note {{requested here}}
55
56 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an rvalue reference or a CopyConstructible type"}}
57 // expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}}
58 any_cast<int&>(42);
59 }
60
main(int,char **)61 int main(int, char**)
62 {
63 test_const_lvalue_cast_request_non_const_lvalue();
64 test_lvalue_any_cast_request_rvalue();
65 test_rvalue_any_cast_request_lvalue();
66
67 return 0;
68 }
69