From patchwork Fri Aug 26 15:21:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 787 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp284299wro; Fri, 26 Aug 2022 08:23:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR5W8iJ5/FrXpAefXjHcDuiOO5E6/1kty7d4noicno1vXdGiAYAx0avS64igZ+ahya2gNYx4 X-Received: by 2002:a17:907:2c74:b0:73d:5dfd:b222 with SMTP id ib20-20020a1709072c7400b0073d5dfdb222mr5668745ejc.6.1661527413210; Fri, 26 Aug 2022 08:23:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661527413; cv=none; d=google.com; s=arc-20160816; b=pyWH6itHRE/NyQT9eRc/qE+rfROH2VbBplOS5/gaiM78R0ESMwexz2Y/+tkSE6/G8u 96rLbTdCWE7BhIPaR/RPIgCiAE/Y8tHzkcyEDCNxTp1TvvdQW1CDq7MhLVhJNA2nDR5A YvRepb4kNNBAEV/BeD11t2cgZK+sGoTbgmoQQGT7Olx5p7A0GK92W68wRyrmkFWQk0b5 wOdEsxGNRLgBY8wnbBGBXIqCp5n+YyaGX9alTyMxyKn1RhPwIgvjJx4Hfv2+2TtyNrau wvo7gqoNpiDELeauycHMsuqdxQrDhVD+pEtpz/JwVk/XEqP1GSXPT/2YljteDRIyVTwA pR+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=VmPNtmB53cMeLbS/O6oldhBpz1ryNTvcKvmEW4zkAWI=; b=zCqurk70vjIJ+OopnVV03S+/AGYKg0P2cG6Sc7eEfYpbnN8aHmbmiOv5dVqLCJwUfx lkr9M6k+VL/wYCtSfhE77KJ5dDSfVkuE6C0wr8JxNS+IB7OBz0DIigm9PnH+uAoFRxQm ct14rsqONV+PdSXvA/6qpke7qetO7zwUowC8y09+ouQSYwbDHlBcMIreJ61MYGcn2xpV ZX9Fj7daMQ+RInKzrUa4YLp46bd+8vRI8FNtg6TTZg1EFDtuBZAAwXhJFGC68bxWYW++ Q8jZ5JyZvH/FL7uerA9kT0piZTJiFnXXpzX8grrGdbiVlHgQllMcBC/gSGM/v5e3y7sJ l18A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=s+SOQ2rW; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ga26-20020a1709070c1a00b0073ddec832bdsi1520545ejc.911.2022.08.26.08.23.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 08:23:33 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=s+SOQ2rW; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 38EC83851AA9 for ; Fri, 26 Aug 2022 15:23:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38EC83851AA9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661527412; bh=VmPNtmB53cMeLbS/O6oldhBpz1ryNTvcKvmEW4zkAWI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=s+SOQ2rWpSXcovkfrDoV1QcoudXld9YOiDlRYHsFeBGP/336idagya9vQEoIrQJ0d KdSFQcUak4cVNwhT9Csrq08ErkaYuao8qf8zsHK1rwVKigbM7quqI2TmS13i9Sachf U50G6nQVJ4s2iAnXTcSmKQmgu5g1ZyDPGop7vrD8= 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 ESMTPS id 0F290385041E for ; Fri, 26 Aug 2022 15:21:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0F290385041E Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-383-xOzYbYFPMein6w19AYpR-g-1; Fri, 26 Aug 2022 11:21:32 -0400 X-MC-Unique: xOzYbYFPMein6w19AYpR-g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 15EF53801F67; Fri, 26 Aug 2022 15:21:32 +0000 (UTC) Received: from localhost (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF9D14010D42; Fri, 26 Aug 2022 15:21:31 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Simplify std::error_code and std::error_condition Date: Fri, 26 Aug 2022 16:21:31 +0100 Message-Id: <20220826152131.166998-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742237768389965508?= X-GMAIL-MSGID: =?utf-8?q?1742237768389965508?= Tested powerpc64le-linux, pushed to trunk. -- >8 -- This removes the redundant operator=(E) from std::error_code and std::error_condition. Without that overload, assignment from a custom type will use the templated constructor to create a temporary and then use the trivial copy assignment operator. With the overloaded assignment, we have to check the constraints twice as often, because that overload and its constraints are checked for simple copy assignments (including the one in the overloaded assignment operator itself!) Also add tests that ADL is used as per LWG 3629. libstdc++-v3/ChangeLog: * include/std/system_error (error_code::_Check): New alias template for constructor SFINAE constraint. (error_code::error_code(ErrorCodeEnum)): Use it. (error_code::operator=(ErrorCodeEnum)): Remove. (error_condition::_Check): New alias template for constraint. (error_condition::error_condition(ErrorConditionEnum)): Use it. (error_condition::operator=(ErrorConditionEnum)): Remove. * testsuite/19_diagnostics/error_code/cons/1.cc: Check constructor taking user-defined error enum. * testsuite/19_diagnostics/error_condition/cons/1.cc: Likewise. --- libstdc++-v3/include/std/system_error | 38 ++++++++++--------- .../19_diagnostics/error_code/cons/1.cc | 31 +++++++++++++-- .../19_diagnostics/error_condition/cons/1.cc | 31 +++++++++++++-- 3 files changed, 75 insertions(+), 25 deletions(-) diff --git a/libstdc++-v3/include/std/system_error b/libstdc++-v3/include/std/system_error index 87cf720f6e3..050439427cc 100644 --- a/libstdc++-v3/include/std/system_error +++ b/libstdc++-v3/include/std/system_error @@ -216,6 +216,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) */ class error_code { + template + using _Check + = __enable_if_t::value>; + public: error_code() noexcept : _M_value(0), _M_cat(&system_category()) { } @@ -223,11 +227,15 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) error_code(int __v, const error_category& __cat) noexcept : _M_value(__v), _M_cat(&__cat) { } - template::value>::type> + /// Initialize with a user-defined type, by calling make_error_code. + template> error_code(_ErrorCodeEnum __e) noexcept { *this = make_error_code(__e); } + error_code(const error_code&) = default; + error_code& operator=(const error_code&) = default; + void assign(int __v, const error_category& __cat) noexcept { @@ -239,13 +247,6 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) clear() noexcept { assign(0, system_category()); } - // DR 804. - template - typename enable_if::value, - error_code&>::type - operator=(_ErrorCodeEnum __e) noexcept - { return *this = make_error_code(__e); } - /// The error value. [[__nodiscard__]] int @@ -345,6 +346,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) */ class error_condition { + template + using _Check + = __enable_if_t::value>; + public: /// Initialize with a zero (no error) value and the generic category. error_condition() noexcept @@ -354,11 +359,15 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) error_condition(int __v, const error_category& __cat) noexcept : _M_value(__v), _M_cat(&__cat) { } - template::value>::type> + /// Initialize with a user-defined type, by calling make_error_condition. + template> error_condition(_ErrorConditionEnum __e) noexcept { *this = make_error_condition(__e); } + error_condition(const error_condition&) = default; + error_condition& operator=(const error_condition&) = default; + /// Set the value and category. void assign(int __v, const error_category& __cat) noexcept @@ -367,13 +376,6 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) _M_cat = &__cat; } - // DR 804. - template - typename enable_if::value, error_condition&>::type - operator=(_ErrorConditionEnum __e) noexcept - { return *this = make_error_condition(__e); } - /// Reset the value and category to the default-constructed state. void clear() noexcept diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_code/cons/1.cc b/libstdc++-v3/testsuite/19_diagnostics/error_code/cons/1.cc index f454fc270d8..d48d5c1ffe8 100644 --- a/libstdc++-v3/testsuite/19_diagnostics/error_code/cons/1.cc +++ b/libstdc++-v3/testsuite/19_diagnostics/error_code/cons/1.cc @@ -22,23 +22,46 @@ #include #include +namespace adl +{ + struct Error { }; + + const Error err; + + struct category : std::error_category + { + const char* name() const noexcept override { return "adl"; } + std::string message(int) const { return ""; } + }; + + const category cat; + + std::error_code + make_error_code(Error) { return std::error_code(999, cat); } +} + +template<> struct std::is_error_code_enum : std::true_type { }; + int main() { - // 1 + // 1 error_code() std::error_code e1; VERIFY( e1.value() == 0 ); VERIFY( e1.category() == std::system_category() ); - // 2 + // 2 error_code(int, const error_category&) const __gnu_test::test_category cat; std::error_code e2(e1.value(), cat); VERIFY( e2.value() == e1.value() ); VERIFY( e2.category() == cat ); - // 3 + // 3 error_code(const error_code&) std::error_code e3(std::make_error_code(std::errc::operation_not_supported)); VERIFY( e3.value() == int(std::errc::operation_not_supported) ); VERIFY( e3.category() == std::generic_category() ); - return 0; + // 4 error_code(ErrorCodeEnum) + std::error_code e4(adl::err); + VERIFY( e4.value() == 999 ); + VERIFY( e4.category() == adl::cat ); } diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/1.cc b/libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/1.cc index 1e039c45b1d..c353b0266ee 100644 --- a/libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/1.cc +++ b/libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/1.cc @@ -21,23 +21,48 @@ #include #include +namespace adl +{ + struct Error { }; + + const Error err; + + struct category : std::error_category + { + const char* name() const noexcept override { return "adl"; } + std::string message(int) const { return ""; } + }; + + const category cat; + + std::error_condition + make_error_condition(Error) { return std::error_condition(999, cat); } +} + +template<> struct std::is_error_condition_enum : std::true_type { }; + void test01() { - // 1 + // 1 error_condition() std::error_condition e1; VERIFY( e1.value() == 0 ); VERIFY( e1.category() == std::generic_category() ); - // 2 + // 2 error_condition(int, const error_category&) const __gnu_test::test_category cat; std::error_condition e2(e1.value(), cat); VERIFY( e2.value() == e1.value() ); VERIFY( e2.category() == cat ); - // 3 + // 3 error_condition(const error_condition&) std::error_condition e3(std::errc::operation_not_supported); VERIFY( e3.value() == int(std::errc::operation_not_supported) ); VERIFY( e3.category() == std::generic_category() ); + + // 4 error_condition(ErrorConditionEnum) + std::error_condition e4(adl::err); + VERIFY( e4.value() == 999 ); + VERIFY( e4.category() == adl::cat ); } int main()