Skip to content

Commit eab6ab6

Browse files
committed
Add test for variant_CONFIG_OVERRIDE_BAD_VARIANT_ACCESS, variant_CONFIG_OVERRIDE_MONOSTATE
1 parent 81fa50d commit eab6ab6

File tree

5 files changed

+91
-4
lines changed

5 files changed

+91
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ variant_alternative_t<>: Allows to select type by index (C++11)
484484
variant_alternative_T(): Allows to select type by index (non-standard: macro)
485485
std::hash<>: Allows to obtain hash (C++11)
486486
tweak header: reads tweak header if supported [tweak]
487+
bad_variant_access: Allows to override nonstd::bad_variant_access via variant_CONFIG_OVERRIDE_MONOSTATE
487488
```
488489

489490
</p>

test/t.bat

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ set unit_config=
2424

2525
set msvc_defines=^
2626
-D_CRT_SECURE_NO_WARNINGS ^
27-
-D_SCL_SECURE_NO_WARNINGS
27+
-D_SCL_SECURE_NO_WARNINGS ^
28+
-D_SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING
2829

2930
set CppCoreCheckInclude=%VCINSTALLDIR%\Auxiliary\VS\include
3031

31-
cl -nologo -W3 -EHsc %std% %unit_select% %unit_config% %msvc_defines% -I"%CppCoreCheckInclude%" -Ilest -I../include -I. %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
32+
cl -nologo -W3 -EHsc %std% %unit_select% %unit_config% %msvc_defines% -I"%CppCoreCheckInclude%" -Ilest -I../include -I. %unit%-main.t.cpp %unit%.t.cpp %unit%-override.t.cpp && %unit%-main.t.exe
3233
endlocal & goto :EOF
3334

3435
:: subroutines:

test/tg-all.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
@for %%s in ( c++98 c++03 c++11 c++14 c++17 ) do (
1+
@for %%s in ( c++98 c++03 c++11 c++14 c++17 c++23 ) do (
22
call tg.bat %%s
33
)

test/tg.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ rem -flto / -fwhole-program
2929
set optflags=-O2
3030
set warnflags=-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wno-padded -Wno-missing-noreturn
3131

32-
%gpp% -std=%std% %optflags% %warnflags% %unit_select% %unit_config% -o %unit%-main.t.exe -isystem lest -I../include -I. %unit%-main.t.cpp %unit%.t.cpp && %unit%-main.t.exe
32+
%gpp% -std=%std% %optflags% %warnflags% %unit_select% %unit_config% -o %unit%-main.t.exe -isystem lest -I../include -I. %unit%-main.t.cpp %unit%.t.cpp %unit%-override.t.cpp && %unit%-main.t.exe
3333

3434
endlocal & goto :EOF
3535

test/variant-override.t.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)