Skip to content

Commit 67b8c3f

Browse files
committed
Mark bad_optional_access as [[nodiscard]] (default), control via optional_CONFIG_NO_NODISCARD (nonstd-lite-project issue 74)
Add configuration option `optional_CONFIG_NO_NODISCARD`. nonstd-lite/nonstd-lite#74
1 parent d00c714 commit 67b8c3f

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
@@ -247,6 +247,11 @@ Define this to 1 if you want to compile without extensions. Default is undefined
247247
-D<b>optional\_CONFIG\_NO\_EXCEPTIONS</b>=0
248248
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.
249249
250+
#### Disable \[\[nodiscard\]\]
251+
252+
-D<b>optional\_CONFIG\_NO\_NODISCARD</b>=0
253+
Define this to 1 if you want to compile without \[\[nodiscard\]\]. Note that the default of marking functions and `class bad_optional_access` 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.
254+
250255
#### Macros to control alignment
251256
252257
If *optional lite* is compiled as C++11 or later, C++11 alignment facilities are used for storage of the underlying object. When compiled as pre-C++11, *optional lite* tries to determine proper alignment itself. If this doesn't work out, you can control alignment via the following macros. See also section [Implementation notes](#implementation-notes).

include/nonstd/optional.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
#define optional_CONFIG_NO_EXTENSIONS 0
5151
#endif
5252

53+
// Control marking class bad_optional_access and several methods with [[nodiscard]]]:
54+
55+
#if !defined(optional_CONFIG_NO_NODISCARD)
56+
# define optional_CONFIG_NO_NODISCARD 0
57+
#else
58+
# define optional_CONFIG_NO_NODISCARD 1
59+
#endif
60+
5361
// Control presence of exception handling (try and auto discover):
5462

5563
#ifndef optional_CONFIG_NO_EXCEPTIONS
@@ -386,7 +394,7 @@ namespace nonstd {
386394
# define optional_constexpr14 /*constexpr*/
387395
#endif
388396

389-
#if optional_HAVE( NODISCARD )
397+
#if optional_HAVE( NODISCARD ) && !optional_CONFIG_NO_NODISCARD
390398
# define optional_nodiscard [[nodiscard]]
391399
#else
392400
# define optional_nodiscard /*[[nodiscard]]*/
@@ -917,7 +925,7 @@ const nullopt_t nullopt(( nullopt_t::init() ));
917925

918926
#if ! optional_CONFIG_NO_EXCEPTIONS
919927

920-
class bad_optional_access : public std::logic_error
928+
class optional_nodiscard bad_optional_access : public std::logic_error
921929
{
922930
public:
923931
explicit bad_optional_access()

0 commit comments

Comments
 (0)