1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
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
43 // On platforms that do not support any_cast, an additional availability error
44 // is triggered by these tests.
45 // expected-error@not_copy_constructible.fail.cpp:* 0+ {{call to unavailable function 'any_cast': introduced in macOS 10.14}}
46
main()47 int main() {
48 any a;
49 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an lvalue reference or a CopyConstructible type"}}
50 // expected-error@any:* {{static_cast from 'no_copy' to 'no_copy' uses deleted function}}
51 any_cast<no_copy>(static_cast<any&>(a)); // expected-note {{requested here}}
52
53 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}}
54 // expected-error@any:* {{static_cast from 'const no_copy' to 'no_copy' uses deleted function}}
55 any_cast<no_copy>(static_cast<any const&>(a)); // expected-note {{requested here}}
56
57 any_cast<no_copy>(static_cast<any &&>(a)); // OK
58
59 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be an rvalue reference or a CopyConstructible type"}}
60 // expected-error@any:* {{static_cast from 'typename remove_reference<no_move &>::type' (aka 'no_move') to 'no_move' uses deleted function}}
61 any_cast<no_move>(static_cast<any &&>(a));
62 }
63