Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions source/declarations.tex
Original file line number Diff line number Diff line change
Expand Up @@ -9004,12 +9004,26 @@
or with different \grammarterm{attribute-argument-clause}s)
are allowed.

\pnum
A \defnadj{nodiscard}{type} is
a (possibly cv-qualified) class or enumeration type
marked \tcode{nodiscard} in a reachable declaration.
A \defnadj{nodiscard}{call} is either
\begin{itemize}
\item
a function call expression\iref{expr.call}
that calls a function declared \tcode{nodiscard} in a reachable declaration or
whose return type is a nodiscard type, or
\item
an explicit type
conversion~(\ref{expr.static.cast}, \ref{expr.cast}, \ref{expr.type.conv})
that constructs an object through a constructor declared \tcode{nodiscard}, or
that initializes an object of a nodiscard type.
\end{itemize}

\pnum
\begin{note}
A nodiscard call is a function call expression that
calls a function previously declared \tcode{nodiscard}, or
whose return type is a possibly cv-qualified class or enumeration type
marked \tcode{nodiscard}. Appearance of a nodiscard call as
Appearance of a nodiscard call as
a potentially-evaluated discarded-value expression\iref{expr.prop}
is discouraged unless explicitly cast to \tcode{void}.
Implementations should issue a warning in such cases.
Expand All @@ -9024,10 +9038,22 @@
\pnum
\begin{example}
\begin{codeblock}
struct [[nodiscard]] my_scopeguard { @\commentellip@ };
struct my_unique {
my_unique() = default; // does not acquire resource
[[nodiscard]] my_unique(int fd) { @\commentellip@ } // acquires resource
~my_unique() noexcept { @\commentellip@ } // releases resource, if any
@\commentellip@
};
struct [[nodiscard]] error_info { @\commentellip@ };
error_info enable_missile_safety_mode();
void launch_missiles();
void test_missiles() {
my_scopeguard(); // warning encouraged
(void)my_scopeguard(), // warning not encouraged, cast to \tcode{void}
launch_missiles(); // comma operator, statement continues
my_unique(42); // warning encouraged
my_unique(); // warning not encouraged
enable_missile_safety_mode(); // warning encouraged
launch_missiles();
}
Expand Down