Skip to content

Commit da9b8ca

Browse files
Add the ability to use external definitions for monostate and bad variant access (#55, thanks @MostafaNanticock)
* Add support for configurable monostate type Introduced variant_CONFIG_MONOSTATE that users can set its value if they want variant-lite to use it instead of its own monostate class * Add support for configurable bad_variant_access type Introduced variant_CONFIG_BAD_VARIANT_ACCESS that users can set its value if they want variant-lite to use it instead of its own bad_variant_access class * Update README.md synopsis: Configuration macros Added `variant_CONFIG_BAD_VARIANT_ACCESS` and `variant_CONFIG_MONOSTATE` to the Synopsis: Configuration macros section * Updated template/variant.hpp * Fixed: std::hash support with variant_CONFIG_MONOSTATE
1 parent f403501 commit da9b8ca

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,32 @@ Define this to the pod-type you want to align to (no default).
248248
\-D<b>variant\_CONFIG\_ALIGN\_AS\_FALLBACK</b>=*pod_type*
249249
Define this to the pod-type to use for alignment if the algorithm of *variant lite* cannot find a suitable POD type to use for alignment. Default is `double`.
250250
251+
#### Override `bad_variant_access`
252+
<b>variant_CONFIG_BAD_VARIANT_ACCESS</b>=type
253+
254+
Define this macro to override the default definition of `bad_variant_access`.
255+
256+
This is useful when integrating with other compatibility libraries or the standard library to avoid conflicting exception types.
257+
258+
Example:
259+
```cpp
260+
#define variant_CONFIG_BAD_VARIANT_ACCESS std::bad_variant_access
261+
```
262+
If not defined, variant-lite uses its own `nonstd::bad_variant_access`.
263+
264+
#### Override `monostate`
265+
<b>variant_CONFIG_MONOSTATE</b>=type
266+
267+
Define this macro to override the default `monostate` type used by variant-lite.
268+
269+
This helps prevent type conflicts when multiple libraries define their own monostate.
270+
271+
Example:
272+
```cpp
273+
#define variant_CONFIG_MONOSTATE std::monostate
274+
```
275+
If not defined, variant-lite uses its own `nonstd::monostate`.
276+
251277
## Reported to work with
252278
253279
The table below mentions the compiler versions *variant lite* is reported to work with.

include/nonstd/variant.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,16 +1212,19 @@ class variant;
12121212

12131213
// 19.7.8 Class monostate
12141214

1215+
#ifdef variant_CONFIG_MONOSTATE
1216+
using variant_CONFIG_MONOSTATE;
1217+
#else
12151218
class monostate{};
12161219

12171220
// 19.7.9 monostate relational operators
1218-
12191221
inline variant_constexpr bool operator< ( monostate, monostate ) variant_noexcept { return false; }
12201222
inline variant_constexpr bool operator> ( monostate, monostate ) variant_noexcept { return false; }
12211223
inline variant_constexpr bool operator<=( monostate, monostate ) variant_noexcept { return true; }
12221224
inline variant_constexpr bool operator>=( monostate, monostate ) variant_noexcept { return true; }
12231225
inline variant_constexpr bool operator==( monostate, monostate ) variant_noexcept { return true; }
12241226
inline variant_constexpr bool operator!=( monostate, monostate ) variant_noexcept { return false; }
1227+
#endif
12251228

12261229
// 19.7.4 variant helper classes
12271230

@@ -1280,6 +1283,9 @@ static const std::size_t variant_npos = static_cast<std::size_t>( -1 );
12801283

12811284
// 19.7.11 Class bad_variant_access
12821285

1286+
#ifdef variant_CONFIG_BAD_VARIANT_ACCESS
1287+
using variant_CONFIG_BAD_VARIANT_ACCESS;
1288+
#else
12831289
class variant_nodiscard bad_variant_access : public std::exception
12841290
{
12851291
public:
@@ -1292,6 +1298,7 @@ class variant_nodiscard bad_variant_access : public std::exception
12921298
return "bad variant access";
12931299
}
12941300
};
1301+
#endif
12951302

12961303
#endif // variant_CONFIG_NO_EXCEPTIONS
12971304

@@ -2698,7 +2705,7 @@ using namespace variants;
26982705

26992706
} // namespace nonstd
27002707

2701-
#if variant_CPP11_OR_GREATER
2708+
#if ! defined (variant_CONFIG_MONOSTATE) && variant_CPP11_OR_GREATER
27022709

27032710
// 19.7.12 Hash support
27042711

@@ -2746,7 +2753,7 @@ struct hash< nonstd::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T
27462753

27472754
} //namespace std
27482755

2749-
#endif // variant_CPP11_OR_GREATER
2756+
#endif // ! defined (variant_CONFIG_MONOSTATE) && variant_CPP11_OR_GREATER
27502757

27512758
#if variant_BETWEEN( variant_COMPILER_MSVC_VER, 1300, 1900 )
27522759
# pragma warning( pop )

template/variant.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,16 +1087,19 @@ class variant;
10871087

10881088
// 19.7.8 Class monostate
10891089

1090+
#ifdef variant_CONFIG_MONOSTATE
1091+
using variant_CONFIG_MONOSTATE;
1092+
#else
10901093
class monostate{};
10911094

10921095
// 19.7.9 monostate relational operators
1093-
10941096
inline variant_constexpr bool operator< ( monostate, monostate ) variant_noexcept { return false; }
10951097
inline variant_constexpr bool operator> ( monostate, monostate ) variant_noexcept { return false; }
10961098
inline variant_constexpr bool operator<=( monostate, monostate ) variant_noexcept { return true; }
10971099
inline variant_constexpr bool operator>=( monostate, monostate ) variant_noexcept { return true; }
10981100
inline variant_constexpr bool operator==( monostate, monostate ) variant_noexcept { return true; }
10991101
inline variant_constexpr bool operator!=( monostate, monostate ) variant_noexcept { return false; }
1102+
#endif // variant_CONFIG_MONOSTATE
11001103

11011104
// 19.7.4 variant helper classes
11021105

@@ -1155,6 +1158,9 @@ static const std::size_t variant_npos = static_cast<std::size_t>( -1 );
11551158

11561159
// 19.7.11 Class bad_variant_access
11571160

1161+
#ifdef variant_CONFIG_BAD_VARIANT_ACCESS
1162+
using variant_CONFIG_BAD_VARIANT_ACCESS;
1163+
#else
11581164
class variant_nodiscard bad_variant_access : public std::exception
11591165
{
11601166
public:
@@ -1167,6 +1173,7 @@ class variant_nodiscard bad_variant_access : public std::exception
11671173
return "bad variant access";
11681174
}
11691175
};
1176+
#endif // variant_CONFIG_BAD_VARIANT_ACCESS
11701177

11711178
#endif // variant_CONFIG_NO_EXCEPTIONS
11721179

@@ -2043,7 +2050,7 @@ using namespace variants;
20432050

20442051
} // namespace nonstd
20452052

2046-
#if variant_CPP11_OR_GREATER
2053+
#if ! defined (variant_CONFIG_MONOSTATE) && variant_CPP11_OR_GREATER
20472054

20482055
// 19.7.12 Hash support
20492056

@@ -2077,7 +2084,7 @@ struct hash< nonstd::variant<{{TplArgsList}}> >
20772084

20782085
} //namespace std
20792086

2080-
#endif // variant_CPP11_OR_GREATER
2087+
#endif // ! defined (variant_CONFIG_MONOSTATE) && variant_CPP11_OR_GREATER
20812088

20822089
#if variant_BETWEEN( variant_COMPILER_MSVC_VER, 1300, 1900 )
20832090
# pragma warning( pop )

0 commit comments

Comments
 (0)