On Mon, 2 Mar 2026 at 16:00, Arthur O'Dwyer <[email protected]> wrote:
>
> The new test includes two lines that currently do not warn because of
> GCC compiler bug PR85973; the lines that do warn are the more
> important cases.

Pushed to trunk - thanks.


>
>         PR libstdc++/119197
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/expected (std::expected): Add [[nodiscard]] to class.
>         * testsuite/20_util/expected/119197.cc: New test.
>
> Signed-off-by: Arthur O'Dwyer <[email protected]>
> ---
>  libstdc++-v3/include/std/expected             |  4 ++--
>  .../testsuite/20_util/expected/119197.cc      | 20 +++++++++++++++++++
>  2 files changed, 22 insertions(+), 2 deletions(-)
>  create mode 100644 libstdc++-v3/testsuite/20_util/expected/119197.cc
>
> diff --git a/libstdc++-v3/include/std/expected
> b/libstdc++-v3/include/std/expected
> index 7ab4e4595fa..21522850d7a 100644
> --- a/libstdc++-v3/include/std/expected
> +++ b/libstdc++-v3/include/std/expected
> @@ -367,7 +367,7 @@ namespace __expected
>  /// @endcond
>
>    template<typename _Tp, typename _Er>
> -    class expected
> +    class [[nodiscard]] expected
>      {
>        static_assert( ! is_reference_v<_Tp> );
>        static_assert( ! is_function_v<_Tp> );
> @@ -1360,7 +1360,7 @@ namespace __expected
>
>    // Partial specialization for std::expected<cv void, E>
>    template<typename _Tp, typename _Er> requires is_void_v<_Tp>
> -    class expected<_Tp, _Er>
> +    class [[nodiscard]] expected<_Tp, _Er>
>      {
>        static_assert( __expected::__can_be_unexpected<_Er> );
>
> diff --git a/libstdc++-v3/testsuite/20_util/expected/119197.cc
> b/libstdc++-v3/testsuite/20_util/expected/119197.cc
> new file mode 100644
> index 00000000000..dd4db6d68f3
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/20_util/expected/119197.cc
> @@ -0,0 +1,20 @@
> +// { dg-do compile { target c++23 } }
> +
> +// PR libstdc++/119197 - std::expected should be nodiscard
> +
> +#include <expected>
> +
> +std::expected<int, int> f();
> +std::expected<void, int> g();
> +
> +void
> +test_nodiscard()
> +{
> +  f();                         // { dg-warning "ignoring return" }
> +  std::expected<int, int>(42); // c++/PR85973: should warn "ignoring 
> temporary"
> +  (void)f();                   // OK
> +
> +  g();                         // { dg-warning "ignoring return" }
> +  std::expected<void, int>();  // c++/PR85973: should warn "ignoring 
> temporary"
> +  (void)g();                   // OK
> +}
> --
> 2.34.1
>

Reply via email to