From patchwork Tue Apr 6 15:54:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1462892 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=NqPX6I28; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FFBtS6nDGz9sRf for ; Wed, 7 Apr 2021 01:54:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7DA063894420; Tue, 6 Apr 2021 15:54:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7DA063894420 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1617724469; bh=OgT4XcyE9DKqn8Z4+u+HraNF0fEo8OqdM3l38g0pEZE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=NqPX6I28nGyvZauYvAj3p6+M91adQInYjZ8cPB86Mdl9cA1PTYrxLGG4bpI+QXKOV vDebwz+6JZrex1v7Gu+DX5qUbJeInui6b7KZGip5UDIj9Jhz3Eoq3vEgqnumKx9iRE /4oJymdMfDOo7UA0iftXU34KVS4ryOE5h0qLatHc= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 8B1D43894418 for ; Tue, 6 Apr 2021 15:54:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8B1D43894418 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-377-q7IKiCvyOiqxN1ECAkgnQw-1; Tue, 06 Apr 2021 11:54:24 -0400 X-MC-Unique: q7IKiCvyOiqxN1ECAkgnQw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2692F10866BC; Tue, 6 Apr 2021 15:54:23 +0000 (UTC) Received: from localhost (unknown [10.33.36.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2F9519D61; Tue, 6 Apr 2021 15:54:22 +0000 (UTC) Date: Tue, 6 Apr 2021 16:54:22 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add nodiscard attribute to cast-like functions Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-14.5 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" Add [[nodiscard]] to functions that are effectively just a static_cast, as per P2351. Also add it to std::addressof. libstdc++-v3/ChangeLog: * include/bits/move.h (forward, move, move_if_noexcept) (addressof): Add _GLIBCXX_NODISCARD. * include/bits/ranges_cmp.h (identity::operator()): Add nodiscard attribute. * include/c_global/cstddef (to_integer): Likewise. * include/std/bit (bit_cast): Likewise. * include/std/utility (as_const, to_underlying): Likewise. Tested powerpc64le-linux. Committed to trunk. commit 406f58e1e38e92e4b881f3666b596843da308783 Author: Jonathan Wakely Date: Tue Apr 6 14:41:29 2021 libstdc++: Add nodiscard attribute to cast-like functions Add [[nodiscard]] to functions that are effectively just a static_cast, as per P2351. Also add it to std::addressof. libstdc++-v3/ChangeLog: * include/bits/move.h (forward, move, move_if_noexcept) (addressof): Add _GLIBCXX_NODISCARD. * include/bits/ranges_cmp.h (identity::operator()): Add nodiscard attribute. * include/c_global/cstddef (to_integer): Likewise. * include/std/bit (bit_cast): Likewise. * include/std/utility (as_const, to_underlying): Likewise. diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h index feacae084c9..3abbb37ceeb 100644 --- a/libstdc++-v3/include/bits/move.h +++ b/libstdc++-v3/include/bits/move.h @@ -72,6 +72,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * This function is used to implement "perfect forwarding". */ template + _GLIBCXX_NODISCARD constexpr _Tp&& forward(typename std::remove_reference<_Tp>::type& __t) noexcept { return static_cast<_Tp&&>(__t); } @@ -83,6 +84,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * This function is used to implement "perfect forwarding". */ template + _GLIBCXX_NODISCARD constexpr _Tp&& forward(typename std::remove_reference<_Tp>::type&& __t) noexcept { @@ -97,6 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @return The parameter cast to an rvalue-reference to allow moving it. */ template + _GLIBCXX_NODISCARD constexpr typename std::remove_reference<_Tp>::type&& move(_Tp&& __t) noexcept { return static_cast::type&&>(__t); } @@ -116,6 +119,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * type is copyable, in which case an lvalue-reference is returned instead. */ template + _GLIBCXX_NODISCARD constexpr typename conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type move_if_noexcept(_Tp& __x) noexcept @@ -136,6 +140,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @return The actual address. */ template + _GLIBCXX_NODISCARD inline _GLIBCXX17_CONSTEXPR _Tp* addressof(_Tp& __r) noexcept { return std::__addressof(__r); } diff --git a/libstdc++-v3/include/bits/ranges_cmp.h b/libstdc++-v3/include/bits/ranges_cmp.h index 3f71d31e5a6..f859a33b2c1 100644 --- a/libstdc++-v3/include/bits/ranges_cmp.h +++ b/libstdc++-v3/include/bits/ranges_cmp.h @@ -47,6 +47,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct identity { template + [[nodiscard]] constexpr _Tp&& operator()(_Tp&& __t) const noexcept { return std::forward<_Tp>(__t); } diff --git a/libstdc++-v3/include/c_global/cstddef b/libstdc++-v3/include/c_global/cstddef index 11c808cab28..13ef7f03c12 100644 --- a/libstdc++-v3/include/c_global/cstddef +++ b/libstdc++-v3/include/c_global/cstddef @@ -169,6 +169,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return __l = __l ^ __r; } template + [[nodiscard]] constexpr _IntegerType to_integer(__byte_op_t<_IntegerType> __b) noexcept { return _IntegerType(__b); } diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit index 8638a02c8a6..fb78578448c 100644 --- a/libstdc++-v3/include/std/bit +++ b/libstdc++-v3/include/std/bit @@ -54,6 +54,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Create a value of type `To` from the bits of `from`. template + [[nodiscard]] constexpr _To bit_cast(const _From& __from) noexcept { diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index fb19d62968f..3e68f682e00 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -386,7 +386,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #define __cpp_lib_as_const 201510 template - constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } + [[nodiscard]] + constexpr add_const_t<_Tp>& + as_const(_Tp& __t) noexcept + { return __t; } template void as_const(const _Tp&&) = delete; @@ -466,6 +469,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #define __cpp_lib_to_underlying 202102L /// Convert an object of enumeration type to its underlying type. template + [[nodiscard]] constexpr underlying_type_t<_Tp> to_underlying(_Tp __value) noexcept { return static_cast>(__value); }