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 const any_cast(any const&);
16 //
17 // template <class ValueType>
18 // ValueType any_cast(any &);
19 //
20 // template <class ValueType>
21 // ValueType any_cast(any &&);
22 
23 // Test instantiating the any_cast with a non-copyable type.
24 
25 #include <any>
26 
27 using std::any;
28 using std::any_cast;
29 
30 struct no_copy
31 {
no_copyno_copy32     no_copy() {}
no_copyno_copy33     no_copy(no_copy &&) {}
34     no_copy(no_copy const &) = delete;
35 };
36 
37 struct no_move {
no_moveno_move38   no_move() {}
39   no_move(no_move&&) = delete;
no_moveno_move40   no_move(no_move const&) {}
41 };
42 
main(int,char **)43 int main(int, char**) {
44     any a;
45     // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an lvalue reference or a CopyConstructible type"}}
46     // expected-error@any:* {{static_cast from 'no_copy' to 'no_copy' uses deleted function}}
47     any_cast<no_copy>(static_cast<any&>(a)); // expected-note {{requested here}}
48 
49     // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}}
50     // expected-error@any:* {{static_cast from 'const no_copy' to 'no_copy' uses deleted function}}
51     any_cast<no_copy>(static_cast<any const&>(a)); // expected-note {{requested here}}
52 
53     any_cast<no_copy>(static_cast<any &&>(a)); // OK
54 
55     // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an rvalue reference or a CopyConstructible type"}}
56     // expected-error@any:* {{static_cast from 'typename remove_reference<no_move &>::type' (aka 'no_move') to 'no_move' uses deleted function}}
57     any_cast<no_move>(static_cast<any &&>(a));
58 
59   return 0;
60 }
61