From patchwork Wed Jul 24 19:53:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1964483 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KGWwfGUO; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WTl9L09M1z1yZw for ; Thu, 25 Jul 2024 05:55:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D4363858428 for ; Wed, 24 Jul 2024 19:55:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 1EFCD3858C48 for ; Wed, 24 Jul 2024 19:54:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1EFCD3858C48 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1EFCD3858C48 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850846; cv=none; b=BKdHixQ48lK34cDkUX9Oi0YEyeyzF7f/ZG3uV7XWApx7bvlecneTLCWYUEtKmEpKGszcr9rxEM79xxihT+KQ2HL3iSTa7mNBjigggBWZm6Gx6uUZJSho+LE8/IktQSxKID5FUEbBRTvC/zcmGwYa0BgvpAGQQQNQdcvUyZ3sdfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850846; c=relaxed/simple; bh=Q5u9ISyB0znXu4+8FqI5rF4u14wdtoMfpbP0JV20RD8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rNFcj9k4M0srn6wdlLVVHV3x7pM6DdO7qE55fuVnVhxIZXv+rmQxTV9jgcdIR8Adu8wmk58VoXgksE7nLX4Ap6Lb4oU0VvkTwYhvyP/ibZkWOPTTOV0VHJ675mHyWqQu1sJmWd7FwFaGUK8RH+MdHqfdh439e1RJBzXp19OX8cs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721850843; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YGDLH1AtApZsJGYp7EkHWFKGLujU/NkQvMUYRz6Z2HM=; b=KGWwfGUOjyEGbzXxtzPyZfTF8ydKqOMv6TW2ZRv/aVTySLoqNL81KjM2TfmkLjRW2x/um7 14gtTuTmU9rIl4SCufpt1dDh3+PNEe5Gck2paiQ+MuA83cnjCpfwQStJh12fa04D3BQz3n Hb+tTlSAcPlamh8RqgyZak31rexG0kI= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-66-qDVh482PPS6i3_TR-n3geg-1; Wed, 24 Jul 2024 15:54:02 -0400 X-MC-Unique: qDVh482PPS6i3_TR-n3geg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 57DE01955D52; Wed, 24 Jul 2024 19:54:01 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A9C341955F6B; Wed, 24 Jul 2024 19:53:59 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] libstdc++: Implement LWG 3836 for std::expected bool conversions Date: Wed, 24 Jul 2024 20:53:28 +0100 Message-ID: <20240724195358.4007988-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- libstdc++-v3/ChangeLog: * include/std/expected (expected): Constrain constructors to prevent problematic bool conversions, as per LWG 3836. * testsuite/20_util/expected/lwg3836.cc: New test. --- libstdc++-v3/include/std/expected | 59 ++++++++++++++----- .../testsuite/20_util/expected/lwg3836.cc | 34 +++++++++++ 2 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/expected/lwg3836.cc diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index 86026c3947a..2594cfe131c 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -314,6 +314,17 @@ namespace __expected __guard.release(); } } + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3836. std::expected conversion constructor + // expected(const expected&) should take precedence over + // expected(U&&) with operator bool + + // If T is cv bool, remove_cvref_t is not a specialization of expected. + template + concept __not_constructing_bool_from_expected + = ! is_same_v, bool> + || ! __is_expected>; } /// @endcond @@ -327,26 +338,41 @@ namespace __expected static_assert( ! __expected::__is_unexpected> ); static_assert( __expected::__can_be_unexpected<_Er> ); - template> + // If T is not cv bool, converts-from-any-cvref> and + // is_constructible, cv expected ref-qual> are false. + template, + typename = remove_cv_t<_Tp>> static constexpr bool __cons_from_expected - = __or_v&>, - is_constructible<_Tp, expected<_Up, _Err>>, - is_constructible<_Tp, const expected<_Up, _Err>&>, - is_constructible<_Tp, const expected<_Up, _Err>>, - is_convertible&, _Tp>, - is_convertible, _Tp>, - is_convertible&, _Tp>, - is_convertible, _Tp>, - is_constructible<_Unex, expected<_Up, _Err>&>, - is_constructible<_Unex, expected<_Up, _Err>>, - is_constructible<_Unex, const expected<_Up, _Err>&>, - is_constructible<_Unex, const expected<_Up, _Err>> + = __or_v&>, + is_constructible<_Tp, expected<_Up, _Gr>>, + is_constructible<_Tp, const expected<_Up, _Gr>&>, + is_constructible<_Tp, const expected<_Up, _Gr>>, + is_convertible&, _Tp>, + is_convertible, _Tp>, + is_convertible&, _Tp>, + is_convertible, _Tp>, + is_constructible<_Unex, expected<_Up, _Gr>&>, + is_constructible<_Unex, expected<_Up, _Gr>>, + is_constructible<_Unex, const expected<_Up, _Gr>&>, + is_constructible<_Unex, const expected<_Up, _Gr>> >; - template + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // If t is cv bool, we know it can be constructed from expected, + // but we don't want to cause the expected(U&&) constructor to be used, + // so we only check the is_constructible, ...> cases. + template + static constexpr bool __cons_from_expected<_Up, _Gr, _Unex, bool> + = __or_v&>, + is_constructible<_Unex, expected<_Up, _Gr>>, + is_constructible<_Unex, const expected<_Up, _Gr>&>, + is_constructible<_Unex, const expected<_Up, _Gr>> + >; + + template constexpr static bool __explicit_conv = __or_v<__not_>, - __not_> + __not_> >; template @@ -445,8 +471,9 @@ namespace __expected template requires (!is_same_v, expected>) && (!is_same_v, in_place_t>) - && (!__expected::__is_unexpected>) && is_constructible_v<_Tp, _Up> + && (!__expected::__is_unexpected>) + && __expected::__not_constructing_bool_from_expected<_Tp, _Up> constexpr explicit(!is_convertible_v<_Up, _Tp>) expected(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp, _Up>) diff --git a/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc b/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc new file mode 100644 index 00000000000..cd029c44963 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc @@ -0,0 +1,34 @@ +// { dg-do run { target c++23 } } + +#include +#include + +constexpr void +test_convert_contained_value_to_bool() +{ + struct BaseError { }; + struct DerivedError : BaseError { }; + + std::expected e = false; + + // Should use expected(const expected&) ctor, not expected(U&&): + std::expected e2 = e; + + // Contained value should be e.value() not static_cast(e): + VERIFY( e2.value() == false ); + + std::expected e3(std::unexpect); + std::expected e4 = e3; + // Should have error, not static_cast(e3): + VERIFY( ! e4.has_value() ); +} + +int main() +{ + test_convert_contained_value_to_bool(); + + static_assert([] { + test_convert_contained_value_to_bool(); + return true; + }()); +} From patchwork Wed Jul 24 19:53:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1964482 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Ai74zYPu; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WTl902fb6z1yZw for ; Thu, 25 Jul 2024 05:54:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8896E385840A for ; Wed, 24 Jul 2024 19:54:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 643AB3858C48 for ; Wed, 24 Jul 2024 19:54:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 643AB3858C48 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 643AB3858C48 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850852; cv=none; b=g9xZxUpsQqECc6IGEzZ927dRpQQRRpHndF8feJxj6WRthjZ5FbreZ/DjhvAVu3tNY2fdMfbRDJASg68di5xdRCePLEIZMcw4qg3vDRzuhHKPiMgMzYZLrLz+wp980miIdPAHGuCo/wh7jkWKlC8aRDUHecwJLHsSa+0+OP5j/yc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850852; c=relaxed/simple; bh=kqHuzz9zfjFCcYYdLzApm7b+VFtUUv+B/k3MSzDRPak=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gYNkc2voh9gN0J7IhQbVAGfnPDHdBb5wkVYuG/4FLb4EkwqByF6cvG4NZ0dwtuOqS74Iy/PwlZpQXljKwT52fIvs3mbo0eurrA3bn9c74fP0LToVEkWVc3ezvrk8W1Demdr1OMAxdosk5LUGRafz4G9GHjdB6C5CtBJdR2lv7UU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721850849; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C5+Q0WZAtwaUEEXei6qd+egPfRzISkikYOiU9Q4ssLc=; b=Ai74zYPuHCQdM8T3baijVobOJF+wTH6GXotGOHvtQLtTsvvBDOl8lVf3OivZFx3TvqCog7 C9YD3A8U+cDAdXJSRKskRmVkTYbkX7SbjDXAiEh2b1iOpiyMTMBtHFEpkneIMuV7k8RB5t aUdfp3pvZefMZnPxuunmJG7Uek1CgDQ= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-b-5YgiV4OfCyM1k1pUvh4w-1; Wed, 24 Jul 2024 15:54:05 -0400 X-MC-Unique: b-5YgiV4OfCyM1k1pUvh4w-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BBF831955D47; Wed, 24 Jul 2024 19:54:04 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2030E1955D45; Wed, 24 Jul 2024 19:54:02 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Implement LWG 3836 for std::optional bool conversions Date: Wed, 24 Jul 2024 20:53:29 +0100 Message-ID: <20240724195358.4007988-2-jwakely@redhat.com> In-Reply-To: <20240724195358.4007988-1-jwakely@redhat.com> References: <20240724195358.4007988-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- libstdc++-v3/ChangeLog: * include/std/optional (optional): Constrain constructors to prevent problematic bool conversions, as per LWG 3836. * testsuite/20_util/optional/cons/lwg3836.cc: New test. --- libstdc++-v3/include/std/optional | 58 ++++++++++++++----- .../20_util/optional/cons/lwg3836.cc | 58 +++++++++++++++++++ 2 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 344be5e44d3..700e7047aba 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -749,16 +749,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline constexpr bool __is_optional_v> = true; + template + using __converts_from_any_cvref = __or_< + is_constructible<_Tp, _Wp&>, is_convertible<_Wp&, _Tp>, + is_constructible<_Tp, _Wp>, is_convertible<_Wp, _Tp>, + is_constructible<_Tp, const _Wp&>, is_convertible, + is_constructible<_Tp, const _Wp>, is_convertible + >; + template - using __converts_from_optional = - __or_&>, - is_constructible<_Tp, optional<_Up>&>, - is_constructible<_Tp, const optional<_Up>&&>, - is_constructible<_Tp, optional<_Up>&&>, - is_convertible&, _Tp>, - is_convertible&, _Tp>, - is_convertible&&, _Tp>, - is_convertible&&, _Tp>>; + using __converts_from_optional + = __converts_from_any_cvref<_Tp, optional<_Up>>; template using __assigns_from_optional = @@ -800,6 +801,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template using _Requires = enable_if_t<__and_v<_Cond...>, bool>; + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3836. std::expected conversion constructor + // expected(const expected&) should take precedence over + // expected(U&&) with operator bool + template> + struct __not_constructing_bool_from_optional + : true_type + { }; + + template + struct __not_constructing_bool_from_optional<_From, bool> + : bool_constant>> + { }; + + template> + struct __construct_from_contained_value + : __not_<__converts_from_optional<_Tp, _From>> + { }; + + template + struct __construct_from_contained_value<_From, bool> + : true_type + { }; + public: using value_type = _Tp; @@ -811,7 +836,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template, __not_tag<_Up>, is_constructible<_Tp, _Up>, - is_convertible<_Up, _Tp>> = true> + is_convertible<_Up, _Tp>, + __not_constructing_bool_from_optional<_Up>> = true> constexpr optional(_Up&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -820,7 +846,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template, __not_tag<_Up>, is_constructible<_Tp, _Up>, - __not_>> = false> + __not_>, + __not_constructing_bool_from_optional<_Up>> = false> explicit constexpr optional(_Up&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -830,7 +857,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, const _Up&>, is_convertible, - __not_<__converts_from_optional<_Tp, _Up>>> = true> + __construct_from_contained_value<_Up>> = true> constexpr optional(const optional<_Up>& __t) noexcept(is_nothrow_constructible_v<_Tp, const _Up&>) @@ -843,7 +870,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, const _Up&>, __not_>, - __not_<__converts_from_optional<_Tp, _Up>>> = false> + __construct_from_contained_value<_Up>> = false> explicit constexpr optional(const optional<_Up>& __t) noexcept(is_nothrow_constructible_v<_Tp, const _Up&>) @@ -856,7 +883,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, _Up>, is_convertible<_Up, _Tp>, - __not_<__converts_from_optional<_Tp, _Up>>> = true> + __construct_from_contained_value<_Up>> = true> constexpr optional(optional<_Up>&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -869,7 +896,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, _Up>, __not_>, - __not_<__converts_from_optional<_Tp, _Up>>> = false> + __construct_from_contained_value<_Up>> = false> explicit constexpr optional(optional<_Up>&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -895,7 +922,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Args...>) : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { } - // Assignment operators. _GLIBCXX20_CONSTEXPR optional& operator=(nullopt_t) noexcept diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc b/libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc new file mode 100644 index 00000000000..816eb1b7def --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc @@ -0,0 +1,58 @@ +// { dg-do run { target c++17 } } + +#include +#include + +constexpr void +test_convert_contained_value_to_bool() +{ + struct False { constexpr operator bool() const { return false; } }; + + False f; + std::optional o = f; + + // Should use optional(const optional&) ctor, not optional(U&&): + std::optional o2 = o; + + // Contained value should be static_cast(f) not static_cast(o): + VERIFY( o2.value() == false ); + + std::optional o3; + std::optional o4 = o3; + // Should have no contained value, not static_cast(o3): + VERIFY( ! o4.has_value() ); +} + +constexpr void +test_convert_contained_value_to_bool_explicit() +{ + struct False { constexpr explicit operator bool() const { return false; } }; + + False f; + std::optional o = f; + + // Should use optional(const optional&) ctor, not optional(U&&): + std::optional o2(o); + + // Contained value should be static_cast(f) not static_cast(o): + VERIFY( o2.value() == false ); + + std::optional o3; + std::optional o4(o3); + // Should have no contained value, not static_cast(o3): + VERIFY( ! o4.has_value() ); +} + +int main() +{ + test_convert_contained_value_to_bool(); + test_convert_contained_value_to_bool_explicit(); + +#if __cpp_lib_optional >= 202106 + static_assert([] { + test_convert_contained_value_to_bool(); + test_convert_contained_value_to_bool_explicit(); + return true; + }()); +#endif +}