xref: /llvm-project/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.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 any_cast(any const &);
15 
16 // Try and cast away const.
17 
18 // This test only checks that we static_assert in any_cast when the
19 // constraints are not respected, however Clang will sometimes emit
20 // additional errors while trying to instantiate the rest of any_cast
21 // following the static_assert. We ignore unexpected errors in
22 // clang-verify to make the test more robust to changes in Clang.
23 // ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error
24 
25 #include <any>
26 
27 struct TestType {};
28 struct TestType2 {};
29 
f()30 void f() {
31     std::any a;
32 
33     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
34     std::any_cast<TestType &>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
35 
36     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
37     std::any_cast<TestType &&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
38 
39     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
40     std::any_cast<TestType2 &>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
41 
42     // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
43     std::any_cast<TestType2 &&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
44 }
45