xref: /llvm-project/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp (revision ec350ad418a24f70c88758259c774a1e11c06d74)
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++03, c++11, c++14
10 
11 // <any>
12 
13 // template <class ValueType>
14 // ValueType const any_cast(any const&);
15 //
16 // template <class ValueType>
17 // ValueType any_cast(any &);
18 //
19 // template <class ValueType>
20 // ValueType any_cast(any &&);
21 
22 // Test instantiating the any_cast with a non-copyable type.
23 
24 // This test only checks that we static_assert in any_cast when the
25 // constraints are not respected, however Clang will sometimes emit
26 // additional errors while trying to instantiate the rest of any_cast
27 // following the static_assert. We ignore unexpected errors in
28 // clang-verify to make the test more robust to changes in Clang.
29 // ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error
30 
31 #include <any>
32 
33 struct no_copy {
no_copyno_copy34     no_copy() {}
no_copyno_copy35     no_copy(no_copy &&) {}
36     no_copy(no_copy const &) = delete;
37 };
38 
39 struct no_move {
no_moveno_move40     no_move() {}
41     no_move(no_move&&) = delete;
no_moveno_move42     no_move(no_move const&) {}
43 };
44 
f()45 void f() {
46     std::any a;
47     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
48     std::any_cast<no_copy>(static_cast<std::any&>(a)); // expected-note {{requested here}}
49 
50     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
51     std::any_cast<no_copy>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
52 
53     std::any_cast<no_copy>(static_cast<std::any &&>(a)); // OK
54 
55     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
56     std::any_cast<no_move>(static_cast<std::any &&>(a));
57 }
58