//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14 // // template // any& operator=(ValueType&&); // Test value copy and move assignment. #include #include #include "any_helpers.h" #include "count_new.h" #include "test_macros.h" template void test_assign_value() { assert(LHS::count == 0); assert(RHS::count == 0); LHS::reset(); RHS::reset(); { std::any lhs = LHS(1); const std::any rhs = RHS(2); assert(LHS::count == 1); assert(RHS::count == 1); assert(RHS::copied == 0); lhs = rhs; assert(RHS::copied == 1); assert(LHS::count == 0); assert(RHS::count == 2); assertContains(lhs, 2); assertContains(rhs, 2); } assert(LHS::count == 0); assert(RHS::count == 0); LHS::reset(); RHS::reset(); { std::any lhs = LHS(1); std::any rhs = RHS(2); assert(LHS::count == 1); assert(RHS::count == 1); assert(RHS::moved == 1); lhs = std::move(rhs); assert(RHS::moved >= 1); assert(RHS::copied == 0); assert(LHS::count == 0); assert(RHS::count == 1 + rhs.has_value()); LIBCPP_ASSERT(!rhs.has_value()); assertContains(lhs, 2); if (rhs.has_value()) assertContains(rhs, 0); } assert(LHS::count == 0); assert(RHS::count == 0); } template void test_assign_value_empty() { assert(RHS::count == 0); RHS::reset(); { std::any lhs; RHS rhs(42); assert(RHS::count == 1); assert(RHS::copied == 0); lhs = rhs; assert(RHS::count == 2); assert(RHS::copied == 1); assert(RHS::moved >= 0); assertContains(lhs, 42); } assert(RHS::count == 0); RHS::reset(); { std::any lhs; RHS rhs(42); assert(RHS::count == 1); assert(RHS::moved == 0); lhs = std::move(rhs); assert(RHS::count == 2); assert(RHS::copied == 0); assert(RHS::moved >= 1); assertContains(lhs, 42); } assert(RHS::count == 0); RHS::reset(); } template void test_assign_throws() { #if !defined(TEST_HAS_NO_EXCEPTIONS) auto try_throw = [](std::any& lhs, Tp& rhs) { try { Move ? lhs = std::move(rhs) : lhs = rhs; assert(false); } catch (const my_any_exception&) { // do nothing } catch (...) { assert(false); } }; // const lvalue to empty { std::any lhs; Tp rhs(1); assert(Tp::count == 1); try_throw(lhs, rhs); assert(Tp::count == 1); assertEmpty(lhs); } { std::any lhs = small(2); Tp rhs(1); assert(small::count == 1); assert(Tp::count == 1); try_throw(lhs, rhs); assert(small::count == 1); assert(Tp::count == 1); assertContains(lhs, 2); } { std::any lhs = large(2); Tp rhs(1); assert(large::count == 1); assert(Tp::count == 1); try_throw(lhs, rhs); assert(large::count == 1); assert(Tp::count == 1); assertContains(lhs, 2); } #endif } // Test that any& operator=(ValueType&&) is *never* selected for: // * std::in_place type. // * Non-copyable types void test_sfinae_constraints() { { // Only the constructors are required to SFINAE on in_place_t using Tag = std::in_place_type_t; using RawTag = std::remove_reference_t; static_assert(std::is_assignable::value, ""); } { struct Dummy { Dummy() = delete; }; using T = std::in_place_type_t; static_assert(std::is_assignable::value, ""); } { // Test that the ValueType&& constructor SFINAE's away when the // argument is non-copyable struct NoCopy { NoCopy() = default; NoCopy(NoCopy const&) = delete; NoCopy(NoCopy&&) = default; }; static_assert(!std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } } int main(int, char**) { test_assign_value(); test_assign_value(); test_assign_value(); test_assign_value(); test_assign_value_empty(); test_assign_value_empty(); test_assign_throws(); test_assign_throws(); test_assign_throws(); test_sfinae_constraints(); return 0; }