2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_VARIANT_TEST_HELPERS_H
10 #define SUPPORT_VARIANT_TEST_HELPERS_H
12 #include <type_traits>
16 #include "test_macros.h"
18 #if TEST_STD_VER <= 14
19 #error This file requires C++17
22 // FIXME: Currently the variant<T&> tests are disabled using this macro.
23 #define TEST_VARIANT_HAS_NO_REFERENCES
24 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
25 # define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
28 #ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
29 constexpr bool VariantAllowsNarrowingConversions
= true;
31 constexpr bool VariantAllowsNarrowingConversions
= false;
34 #ifndef TEST_HAS_NO_EXCEPTIONS
36 CopyThrows() = default;
37 CopyThrows(CopyThrows
const&) { throw 42; }
38 CopyThrows
& operator=(CopyThrows
const&) { throw 42; }
43 MoveThrows() { ++alive
; }
44 MoveThrows(MoveThrows
const&) {++alive
;}
45 MoveThrows(MoveThrows
&&) { throw 42; }
46 MoveThrows
& operator=(MoveThrows
const&) { return *this; }
47 MoveThrows
& operator=(MoveThrows
&&) { throw 42; }
48 ~MoveThrows() { --alive
; }
51 int MoveThrows::alive
= 0;
55 MakeEmptyT() { ++alive
; }
56 MakeEmptyT(MakeEmptyT
const&) {
58 // Don't throw from the copy constructor since variant's assignment
59 // operator performs a copy before committing to the assignment.
61 MakeEmptyT(MakeEmptyT
&&) {
64 MakeEmptyT
& operator=(MakeEmptyT
const&) {
67 MakeEmptyT
& operator=(MakeEmptyT
&&) {
70 ~MakeEmptyT() { --alive
; }
72 static_assert(std::is_swappable_v
<MakeEmptyT
>, ""); // required for test
74 int MakeEmptyT::alive
= 0;
76 template <class Variant
>
77 void makeEmpty(Variant
& v
) {
78 Variant
v2(std::in_place_type
<MakeEmptyT
>);
83 assert(v
.valueless_by_exception());
86 #endif // TEST_HAS_NO_EXCEPTIONS
89 #endif // SUPPORT_VARIANT_TEST_HELPERS_H