Skip to content

Commit d426ef2

Browse files
committed
Mark expected as [[nodiscard]] (default), control via nsel_CONFIG_NO_NODISCARD (#74, thanks Quuxplusone)
1 parent 5cea391 commit d426ef2

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ Define this to 1 if you want to compile without exceptions. If not defined, the
129129
-D<b>nsel\_CONFIG\_NO\_EXCEPTIONS\_SEH</b>=0
130130
Define this to 1 or 0 to control the use of SEH when C++ exceptions are disabled (see above). If not defined, the header tries and detect if SEH is available if C++ exceptions have been disabled (e.g. via `-fno-exceptions` or `/kernel`). Default determined in header.
131131
132+
#### Disable \[\[nodiscard\]\]
133+
134+
-D<b>nsel\_CONFIG\_NO\_NODISCARD</b>=0
135+
Define this to 1 if you want to compile without \[\[nodiscard\]\]. Note that the default of marking `class expected` with \[\[nodiscard\]\] is not part of the C++23 standard. The rationale to use \[\[nodiscard\]\] is that unnoticed discarded expected error values may break the error handling flow.
136+
132137
#### Enable compilation errors
133138
134139
\-D<b>nsel\_CONFIG\_CONFIRMS\_COMPILATION\_ERRORS</b>=0

include/nonstd/expected.hpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@
8989
# define nsel_CONFIG_WIN32_LEAN_AND_MEAN 0
9090
#endif
9191

92+
// Control marking class expected with [[nodiscard]]]:
93+
94+
#if !defined(nsel_CONFIG_NO_NODISCARD)
95+
# define nsel_CONFIG_NO_NODISCARD 0
96+
#else
97+
# define nsel_CONFIG_NO_NODISCARD 1
98+
#endif
99+
92100
// Control presence of C++ exception handling (try and auto discover):
93101

94102
#ifndef nsel_CONFIG_NO_EXCEPTIONS
@@ -429,6 +437,7 @@ nsel_DISABLE_MSVC_WARNINGS( 26409 )
429437
// Presence of C++17 language features:
430438

431439
#define nsel_HAVE_DEPRECATED nsel_CPP17_000
440+
#define nsel_HAVE_NODISCARD nsel_CPP17_000
432441

433442
// C++ feature usage:
434443

@@ -438,6 +447,12 @@ nsel_DISABLE_MSVC_WARNINGS( 26409 )
438447
# define nsel_deprecated(msg) /*[[deprecated]]*/
439448
#endif
440449

450+
#if nsel_HAVE_NODISCARD && !nsel_CONFIG_NO_NODISCARD
451+
# define nsel_NODISCARD [[nodiscard]]
452+
#else
453+
# define nsel_NODISCARD /*[[nodiscard]]*/
454+
#endif
455+
441456
//
442457
// expected:
443458
//
@@ -459,7 +474,7 @@ namespace std11 {
459474
}
460475

461476
template< class T >
462-
const T * addressof( const T && ) = delete;
477+
const T * addressof( const T && ) = delete;
463478
#endif
464479
} // namespace std11
465480

@@ -1824,10 +1839,10 @@ namespace expected_lite {
18241839

18251840
#if nsel_P0323R <= 2
18261841
template< typename T, typename E = std::exception_ptr >
1827-
class expected
1842+
class nsel_NODISCARD expected
18281843
#else
18291844
template< typename T, typename E >
1830-
class expected
1845+
class nsel_NODISCARD expected
18311846
#endif // nsel_P0323R
18321847
{
18331848
private:

0 commit comments

Comments
 (0)