|
| 1 | +// Copyright 2016-2025 by Martin Moene |
| 2 | +// |
| 3 | +// https://github.com/martinmoene/variant-lite |
| 4 | +// |
| 5 | +// Distributed under the Boost Software License, Version 1.0. |
| 6 | +// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | + |
| 8 | +// mix C++17 std::monostate and std::bad_variant_access with nonstd::variant with configurable such types. |
| 9 | +// msvc: dumpbin /symbols variant-override.t.obj |grep -i bad_variant_access: std::bad_variant_access |
| 10 | +// msvc: dumpbin /symbols variant.t.obj |grep -i bad_variant_access: nonstd::variants::bad_variant_access |
| 11 | + |
| 12 | +// make sure to only override with std::monostate for C++17 and later: |
| 13 | + |
| 14 | +#ifndef variant_ovr_CPLUSPLUS |
| 15 | +# if defined(_MSVC_LANG ) && !defined(__clang__) |
| 16 | +# define variant_ovr_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG ) |
| 17 | +# else |
| 18 | +# define variant_ovr_CPLUSPLUS __cplusplus |
| 19 | +# endif |
| 20 | +#endif |
| 21 | + |
| 22 | +// Requires C++17: |
| 23 | + |
| 24 | +#if variant_ovr_CPLUSPLUS >= 201703L |
| 25 | + |
| 26 | +#define variant_CONFIG_SELECT_VARIANT variant_VARIANT_NONSTD |
| 27 | +#define variant_CONFIG_OVERRIDE_MONOSTATE std::monostate |
| 28 | +#define variant_CONFIG_OVERRIDE_BAD_VARIANT_ACCESS std::bad_variant_access |
| 29 | + |
| 30 | +#include <string> |
| 31 | +#include <utility> // std::monostate |
| 32 | +#include <variant> |
| 33 | + |
| 34 | +#include "variant-main.t.hpp" |
| 35 | + |
| 36 | +namespace std_compat |
| 37 | +{ |
| 38 | + using std::monostate; |
| 39 | + using std::bad_variant_access; |
| 40 | +} |
| 41 | + |
| 42 | +namespace std_compat |
| 43 | +{ |
| 44 | + template <class... Types> |
| 45 | + using variant = nonstd::variant<Types...>; |
| 46 | + |
| 47 | + // using nonstd::bad_variant_access; |
| 48 | + // using nonstd::monostate; |
| 49 | + using nonstd::in_place_type_t; |
| 50 | + using nonstd::in_place_type; |
| 51 | + using nonstd::in_place_index_t; |
| 52 | + using nonstd::in_place_index; |
| 53 | + using nonstd::get; |
| 54 | + using nonstd::get_if; |
| 55 | + using nonstd::holds_alternative; |
| 56 | + using nonstd::visit; |
| 57 | + using nonstd::variant_npos; |
| 58 | + using nonstd::variant_size; |
| 59 | + using nonstd::variant_size_v; |
| 60 | + using nonstd::variant_alternative; |
| 61 | + using nonstd::variant_alternative_t; |
| 62 | +} |
| 63 | + |
| 64 | +#else // variant_ovr_CPLUSPLUS >= 201703L |
| 65 | + |
| 66 | +// configuration without override of monostate, bad_variant access: |
| 67 | +#include "variant-main.t.hpp" |
| 68 | + |
| 69 | +#endif // variant_ovr_CPLUSPLUS >= 201703L |
| 70 | + |
| 71 | +CASE("bad_variant_access: Allows to override nonstd::bad_variant_access via variant_CONFIG_OVERRIDE_MONOSTATE") |
| 72 | +{ |
| 73 | +#if variant_ovr_CPLUSPLUS >= 201703L |
| 74 | + // using namespace std_compat; |
| 75 | + // use namespace std_compat explicitly, as to avoid 'invisible' parameter type-dependent (koenig) lookup |
| 76 | + |
| 77 | + auto var = std_compat::variant<int, std::string>(std_compat::in_place_type<std::string>, std::string("std::variant") ); |
| 78 | + |
| 79 | + EXPECT_THROWS_AS(std_compat::get<int>(var), std_compat::bad_variant_access); |
| 80 | +#else |
| 81 | + EXPECT("bad_variant_access: no override using std::bad_variant_access (no C++17)"); |
| 82 | +#endif // variant_ovr_CPLUSPLUS >= 201703L |
| 83 | +} |
| 84 | + |
| 85 | +// end of file |
0 commit comments