Skip to content

Commit

Permalink
Mark bad_variant_access as [[nodiscard]] (default), control via varia…
Browse files Browse the repository at this point in the history
…nt_CONFIG_NO_NODISCARD (nonstd-lite-project issue 74)

Add configuration option `variant_CONFIG_NO_NODISCARD`.

martinmoene/nonstd-lite-project#74
  • Loading branch information
martinmoene committed Jan 30, 2025
1 parent 7d1dca8 commit b36014f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,19 @@ Define this macro to override the auto-detection of the supported C++ standard,
At default, *variant lite* uses `std::variant` if it is available and lets you use it via namespace `nonstd`. You can however override this default and explicitly request to use `std::variant` or variant lite's `nonstd::variant` as `nonstd::variant` via the following macros.
\-D<b>variant\_CONFIG\_SELECT\_VARIANT</b>=variant_VARIANT_DEFAULT
-D<b>variant\_CONFIG\_SELECT\_VARIANT</b>=variant_VARIANT_DEFAULT
Define this to `variant_VARIANT_STD` to select `std::variant` as `nonstd::variant`. Define this to `variant_VARIANT_NONSTD` to select `nonstd::variant` as `nonstd::variant`. Default is undefined, which has the same effect as defining to `variant_VARIANT_DEFAULT`.
#### Disable exceptions
-D<b>variant_CONFIG_NO_EXCEPTIONS</b>=0
Define this to 1 if you want to compile without exceptions. If not defined, the header tries and detect if exceptions have been disabled (e.g. via `-fno-exceptions`). Default is undefined.
#### Disable \[\[nodiscard\]\]
-D<b>variant\_CONFIG\_NO\_NODISCARD</b>=0
Define this to 1 if you want to compile without \[\[nodiscard\]\]. Note that the default of marking `class bad_variant_access` with \[\[nodiscard\]\] is not part of the C++17 standard. The rationale to use \[\[nodiscard\]\] is that unnoticed discarded access error values may break the error handling flow.
#### Presence of `variant_size_V()` simulation macro
\-D<b>variant\_CONFIG\_OMIT\_VARIANT\_SIZE\_V\_MACRO</b>=0
Expand Down
18 changes: 16 additions & 2 deletions include/nonstd/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
# define variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO 0
#endif

// Control marking class bad_variant_access with [[nodiscard]]]:

#if !defined(variant_CONFIG_NO_NODISCARD)
# define variant_CONFIG_NO_NODISCARD 0
#else
# define variant_CONFIG_NO_NODISCARD 1
#endif

// Control presence of exception handling (try and auto discover):

#ifndef variant_CONFIG_NO_EXCEPTIONS
Expand Down Expand Up @@ -364,7 +372,7 @@ namespace nonstd {

// Presence of C++17 language features:

// no flag
#define variant_HAVE_NODISCARD variant_CPP17_000

// Presence of C++ library features:

Expand Down Expand Up @@ -404,6 +412,12 @@ namespace nonstd {
# define variant_nullptr NULL
#endif

#if variant_HAVE_NODISCARD && !variant_CONFIG_NO_NODISCARD
# define variant_nodiscard [[nodiscard]]
#else
# define variant_nodiscard /*[[nodiscard]]*/
#endif

#if variant_HAVE_OVERRIDE
# define variant_override override
#else
Expand Down Expand Up @@ -1266,7 +1280,7 @@ static const std::size_t variant_npos = static_cast<std::size_t>( -1 );

// 19.7.11 Class bad_variant_access

class bad_variant_access : public std::exception
class variant_nodiscard bad_variant_access : public std::exception
{
public:
#if variant_CPP11_OR_GREATER
Expand Down

0 comments on commit b36014f

Please sign in to comment.