Skip to content

Commit

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

Add configuration option `any_CONFIG_NO_NODISCARD`.

martinmoene/nonstd-lite-project#74
  • Loading branch information
martinmoene committed Jan 29, 2025
1 parent b955918 commit 3c842ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ Define this to `any_ANY_STD` to select `std::any` as `nonstd::any`. Define this
-D<b>any_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>any\_CONFIG\_NO\_NODISCARD</b>=0
Define this to 1 if you want to compile without \[\[nodiscard\]\]. Note that the default of marking `class bad_any_cast` and function `any_cast()` with \[\[nodiscard\]\] is not part of the C++17 standard. The rationale to use \[\[nodiscard\]\] is that unnoticed discarded error values may break the error handling flow.

## Reported to work with

The table below mentions the compiler versions *any lite* is reported to work with.
Expand Down
12 changes: 10 additions & 2 deletions include/nonstd/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
# define any_CONFIG_SELECT_ANY ( any_HAVE_STD_ANY ? any_ANY_STD : any_ANY_NONSTD )
#endif

// Control marking class bad_any_cast and function any_cast with [[nodiscard]]]:

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

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

#ifndef any_CONFIG_NO_EXCEPTIONS
Expand Down Expand Up @@ -312,7 +320,7 @@ namespace nonstd {
# define any_nullptr NULL
#endif

#if any_HAVE_NODISCARD
#if any_HAVE_NODISCARD && !any_CONFIG_NO_NODISCARD
# define any_nodiscard [[nodiscard]]
#else
# define any_nodiscard /*[[nodiscard]]*/
Expand Down Expand Up @@ -415,7 +423,7 @@ namespace detail {

#if ! any_CONFIG_NO_EXCEPTIONS

class bad_any_cast : public std::bad_cast
class any_nodiscard bad_any_cast : public std::bad_cast
{
public:
#if any_CPP11_OR_GREATER
Expand Down

0 comments on commit 3c842ee

Please sign in to comment.