58 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 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 the25// constraints are not respected, however Clang will sometimes emit26// additional errors while trying to instantiate the rest of any_cast27// following the static_assert. We ignore unexpected errors in28// clang-verify to make the test more robust to changes in Clang.29// ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error30 31#include <any>32 33struct no_copy {34 no_copy() {}35 no_copy(no_copy&&) {}36 no_copy(no_copy const&) = delete;37};38 39struct no_move {40 no_move() {}41 no_move(no_move&&) = delete;42 no_move(no_move const&) {}43};44 45void test() {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 (void)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 (void)std::any_cast<no_copy>(static_cast<std::any const&>(a)); // expected-note {{requested here}}52 53 (void)std::any_cast<no_copy>(static_cast<std::any&&>(a)); // OK54 55 // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}56 (void)std::any_cast<no_move>(static_cast<std::any&&>(a));57}58