Skip to content

Commit d4dc044

Browse files
authored
Merge 2019-07 CWG Motion 20
P1771R1 [[nodiscard]] for constructors Fixes #3000.
2 parents 4f163f4 + 813a430 commit d4dc044

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

source/declarations.tex

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9004,12 +9004,26 @@
90049004
or with different \grammarterm{attribute-argument-clause}s)
90059005
are allowed.
90069006

9007+
\pnum
9008+
A \defnadj{nodiscard}{type} is
9009+
a (possibly cv-qualified) class or enumeration type
9010+
marked \tcode{nodiscard} in a reachable declaration.
9011+
A \defnadj{nodiscard}{call} is either
9012+
\begin{itemize}
9013+
\item
9014+
a function call expression\iref{expr.call}
9015+
that calls a function declared \tcode{nodiscard} in a reachable declaration or
9016+
whose return type is a nodiscard type, or
9017+
\item
9018+
an explicit type
9019+
conversion~(\ref{expr.static.cast}, \ref{expr.cast}, \ref{expr.type.conv})
9020+
that constructs an object through a constructor declared \tcode{nodiscard}, or
9021+
that initializes an object of a nodiscard type.
9022+
\end{itemize}
9023+
90079024
\pnum
90089025
\begin{note}
9009-
A nodiscard call is a function call expression that
9010-
calls a function previously declared \tcode{nodiscard}, or
9011-
whose return type is a possibly cv-qualified class or enumeration type
9012-
marked \tcode{nodiscard}. Appearance of a nodiscard call as
9026+
Appearance of a nodiscard call as
90139027
a potentially-evaluated discarded-value expression\iref{expr.prop}
90149028
is discouraged unless explicitly cast to \tcode{void}.
90159029
Implementations should issue a warning in such cases.
@@ -9024,10 +9038,22 @@
90249038
\pnum
90259039
\begin{example}
90269040
\begin{codeblock}
9041+
struct [[nodiscard]] my_scopeguard { @\commentellip@ };
9042+
struct my_unique {
9043+
my_unique() = default; // does not acquire resource
9044+
[[nodiscard]] my_unique(int fd) { @\commentellip@ } // acquires resource
9045+
~my_unique() noexcept { @\commentellip@ } // releases resource, if any
9046+
@\commentellip@
9047+
};
90279048
struct [[nodiscard]] error_info { @\commentellip@ };
90289049
error_info enable_missile_safety_mode();
90299050
void launch_missiles();
90309051
void test_missiles() {
9052+
my_scopeguard(); // warning encouraged
9053+
(void)my_scopeguard(), // warning not encouraged, cast to \tcode{void}
9054+
launch_missiles(); // comma operator, statement continues
9055+
my_unique(42); // warning encouraged
9056+
my_unique(); // warning not encouraged
90319057
enable_missile_safety_mode(); // warning encouraged
90329058
launch_missiles();
90339059
}

0 commit comments

Comments
 (0)