Skip to content

Commit 326136d

Browse files
committed
Mark bad_any_cast as [[nodiscard]] (default), control via any_CONFIG_NO_NODISCARD (nonstd-lite-project issue 74)
Add configuration option `any_CONFIG_NO_NODISCARD`. nonstd-lite/nonstd-lite#74
1 parent b955918 commit 326136d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ Define this to `any_ANY_STD` to select `std::any` as `nonstd::any`. Define this
145145
-D<b>any_CONFIG_NO_EXCEPTIONS</b>=0
146146
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.
147147

148+
#### Disable \[\[nodiscard\]\]
149+
150+
-D<b>any\_CONFIG\_NO\_NODISCARD</b>=0
151+
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.
152+
148153
## Reported to work with
149154

150155
The table below mentions the compiler versions *any lite* is reported to work with.

include/nonstd/any.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
# define any_CONFIG_SELECT_ANY ( any_HAVE_STD_ANY ? any_ANY_STD : any_ANY_NONSTD )
4545
#endif
4646

47+
// Control marking class bad_any_cast and function any_cast with [[nodiscard]]]:
48+
49+
#if !defined(any_CONFIG_NO_NODISCARD)
50+
# define any_CONFIG_NO_NODISCARD 0
51+
#else
52+
# define any_CONFIG_NO_NODISCARD 1
53+
#endif
54+
4755
// Control presence of exception handling (try and auto discover):
4856

4957
#ifndef any_CONFIG_NO_EXCEPTIONS
@@ -312,7 +320,7 @@ namespace nonstd {
312320
# define any_nullptr NULL
313321
#endif
314322

315-
#if any_HAVE_NODISCARD
323+
#if any_HAVE_NODISCARD && !any_CONFIG_NO_NODISCARD
316324
# define any_nodiscard [[nodiscard]]
317325
#else
318326
# define any_nodiscard /*[[nodiscard]]*/
@@ -415,7 +423,7 @@ namespace detail {
415423

416424
#if ! any_CONFIG_NO_EXCEPTIONS
417425

418-
class bad_any_cast : public std::bad_cast
426+
class any_nodiscard bad_any_cast : public std::bad_cast
419427
{
420428
public:
421429
#if any_CPP11_OR_GREATER

0 commit comments

Comments
 (0)