Skip to content

Commit

Permalink
Mark expected as [[nodiscard]] (default), control via nsel_CONFIG_NO_…
Browse files Browse the repository at this point in the history
…NODISCARD (#74, thanks Quuxplusone)
  • Loading branch information
martinmoene committed Dec 10, 2024
1 parent 5cea391 commit d426ef2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ Define this to 1 if you want to compile without exceptions. If not defined, the
-D<b>nsel\_CONFIG\_NO\_EXCEPTIONS\_SEH</b>=0
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.
#### Disable \[\[nodiscard\]\]
-D<b>nsel\_CONFIG\_NO\_NODISCARD</b>=0
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.
#### Enable compilation errors
\-D<b>nsel\_CONFIG\_CONFIRMS\_COMPILATION\_ERRORS</b>=0
Expand Down
21 changes: 18 additions & 3 deletions include/nonstd/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
# define nsel_CONFIG_WIN32_LEAN_AND_MEAN 0
#endif

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

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

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

#ifndef nsel_CONFIG_NO_EXCEPTIONS
Expand Down Expand Up @@ -429,6 +437,7 @@ nsel_DISABLE_MSVC_WARNINGS( 26409 )
// Presence of C++17 language features:

#define nsel_HAVE_DEPRECATED nsel_CPP17_000
#define nsel_HAVE_NODISCARD nsel_CPP17_000

// C++ feature usage:

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

#if nsel_HAVE_NODISCARD && !nsel_CONFIG_NO_NODISCARD
# define nsel_NODISCARD [[nodiscard]]
#else
# define nsel_NODISCARD /*[[nodiscard]]*/
#endif

//
// expected:
//
Expand All @@ -459,7 +474,7 @@ namespace std11 {
}

template< class T >
const T * addressof( const T && ) = delete;
const T * addressof( const T && ) = delete;
#endif
} // namespace std11

Expand Down Expand Up @@ -1824,10 +1839,10 @@ namespace expected_lite {

#if nsel_P0323R <= 2
template< typename T, typename E = std::exception_ptr >
class expected
class nsel_NODISCARD expected
#else
template< typename T, typename E >
class expected
class nsel_NODISCARD expected
#endif // nsel_P0323R
{
private:
Expand Down

0 comments on commit d426ef2

Please sign in to comment.