]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Implement LWG 4366 for std::expected comparisons
authorJonathan Wakely <jwakely@redhat.com>
Thu, 20 Nov 2025 12:19:54 +0000 (12:19 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 24 Nov 2025 14:59:05 +0000 (14:59 +0000)
commit2ef6875115019f4e853406bb124aa9408aeb4bc6
treec76ae597e45ae66e938b20a913a2513420c3c913
parente31e206b397d5c10d8d316a45a657e48b54f2047
libstdc++: Implement LWG 4366 for std::expected comparisons

This modifies the equality comparisons for std::expected so that they do
not use explicit conversions to bool, to match the constraints which are
specified in terms of "convertible to" which implies implicitly
convertible. As a result of those changes, we cannot use logical
expressions with && or || that involve comparisons of the contained
values, because x && (*y == *z) might do the wrong thing if *y == *z
does not return bool.

Also add [[nodiscard]] attributes which were missing.

The new lwg4366.cc testcase is a dg-do run test not dg-do compile,
because the original example won't compile with libstdc++ even after
these fixes. We constrain the std::expected comparison operators with
std::convertible_to<bool> and the pathological Bool type in the issue
doesn't satisfy that concept. So the new test replaces the deleted
explicit conversion oeprator in the issue with one that isn't deleted
but terminates if called. This ensures we don't call it, thus ensuring
that std::expected's comparisons do implicit conversions only.

It's unclear to me whether using the convertible_to concept in
std::expected comparisons is conforming, or if we should switch to an
__implicitly_convertible_to<bool> concept which only uses
std::is_convertible_v<T, bool> and doesn't check for explicit
conversions. That can be addressed separately from this change.

libstdc++-v3/ChangeLog:

* include/std/expected (operator==): Use implicit conversion to
bool and do not use logical && and || with operands of unknown
types. Add nodiscard attributes.
* testsuite/20_util/expected/equality.cc: Test some missing
cases which were not covered previously.
* testsuite/20_util/expected/lwg4366.cc: New test.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/std/expected
libstdc++-v3/testsuite/20_util/expected/equality.cc
libstdc++-v3/testsuite/20_util/expected/lwg4366.cc [new file with mode: 0644]