From: Yihan Wang Date: Sat, 16 Aug 2025 08:43:05 +0000 (+0800) Subject: libstdc++: Implement LWG4222 'expected' constructor from a single value missing a... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=589f3cd1831446485a6c602578177f5d9794d936;p=thirdparty%2Fgcc.git libstdc++: Implement LWG4222 'expected' constructor from a single value missing a constraint libstdc++-v3/ChangeLog: * include/std/expected (expected(U&&)): Add missing constraint as per LWG 4222. * testsuite/20_util/expected/lwg4222.cc: New test. Signed-off-by: Yihan Wang --- diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index 60f1565f15b..4eaaab693e1 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -474,6 +474,7 @@ namespace __expected template> requires (!is_same_v, expected>) && (!is_same_v, in_place_t>) + && (!is_same_v, unexpect_t>) && is_constructible_v<_Tp, _Up> && (!__expected::__is_unexpected>) && __expected::__not_constructing_bool_from_expected<_Tp, _Up> diff --git a/libstdc++-v3/testsuite/20_util/expected/lwg4222.cc b/libstdc++-v3/testsuite/20_util/expected/lwg4222.cc new file mode 100644 index 00000000000..5c107792456 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/lwg4222.cc @@ -0,0 +1,39 @@ +// { dg-do run { target c++23 } } + +// LWG 4222. 'expected' constructor from a single value missing a constraint + +#include +#include +#include + +struct T { + explicit T(auto) {} +}; +struct E { + E(int) {} +}; + +struct V { + explicit constexpr V(std::unexpect_t) {} +}; + +static_assert(!std::is_constructible_v, std::unexpect_t>); +static_assert(!std::is_constructible_v, std::unexpect_t &>); +static_assert(!std::is_constructible_v, std::unexpect_t &&>); +static_assert(!std::is_constructible_v, const std::unexpect_t>); +static_assert(!std::is_constructible_v, const std::unexpect_t &>); +static_assert(!std::is_constructible_v, const std::unexpect_t &&>); + +constexpr bool test() { + std::expected e1(std::in_place, std::unexpect); + VERIFY( e1.has_value() ); + std::expected e2(std::unexpect, std::unexpect); + VERIFY( !e2.has_value() ); + return true; +} + +int main() { + test(); + static_assert(test()); + return 0; +}