From patchwork Thu Nov 4 18:33:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1551137 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=SjaTyp1D; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HlXRJ3SyGz9sP7 for ; Fri, 5 Nov 2021 05:36:44 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4BB6D3857C77 for ; Thu, 4 Nov 2021 18:36:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BB6D3857C77 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636051002; bh=IpAolJkzFxrtBrSX+o7k2zCgajTbRQghZqMLIGg+S6s=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=SjaTyp1DTp5i40AEYLS+R6H+QDJExF0zyWEu0SS5DIwOsErLdyLuxEhNRW+a0MZzG AuOPYVrEYE03P8GWJpPO9ggzOK/Xtq46CRaDczL3QWM7U5BSsghR5Jj35xnZuiTTxo mcDI5cCYiQ3VIsoNS0GzNrh0+9CTpu111zLxGxVM= 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 ESMTPS id 697A4385803F for ; Thu, 4 Nov 2021 18:33:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 697A4385803F 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-490--fRWT0xON3u_BBd3T65Wcg-1; Thu, 04 Nov 2021 14:33:55 -0400 X-MC-Unique: -fRWT0xON3u_BBd3T65Wcg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 38DFC100B780; Thu, 4 Nov 2021 18:33:54 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id D81715D9D3; Thu, 4 Nov 2021 18:33:53 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Consolidate duplicate metaprogramming utilities Date: Thu, 4 Nov 2021 18:33:53 +0000 Message-Id: <20211104183353.2123678-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, committed to trunk. Currently std::variant uses __index_of to find the first occurence of a type in a pack, and __exactly_once to check that there is no other occurrence. We can reuse the __find_uniq_type_in_pack() function for both tasks, and remove the recursive templates used to implement __index_of and __exactly_once. libstdc++-v3/ChangeLog: * include/bits/utility.h (__find_uniq_type_in_pack): Move definition to here, ... * include/std/tuple (__find_uniq_type_in_pack): ... from here. * include/std/variant (__detail__variant::__index_of): Remove. (__detail::__variant::__exactly_once): Define using __find_uniq_type_in_pack instead of __index_of. (get, get_if, variant::__index_of): Likewise. --- libstdc++-v3/include/bits/utility.h | 22 +++++++++ libstdc++-v3/include/std/tuple | 22 --------- libstdc++-v3/include/std/variant | 69 +++++++++++------------------ 3 files changed, 47 insertions(+), 66 deletions(-) diff --git a/libstdc++-v3/include/bits/utility.h b/libstdc++-v3/include/bits/utility.h index c9ffa008217..ec5ed04990b 100644 --- a/libstdc++-v3/include/bits/utility.h +++ b/libstdc++-v3/include/bits/utility.h @@ -102,6 +102,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; #if __cplusplus >= 201402L + + // Return the index of _Tp in _Types, if it occurs exactly once. + // Otherwise, return sizeof...(_Types). + template + constexpr size_t + __find_uniq_type_in_pack() + { + constexpr size_t __sz = sizeof...(_Types); + constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... }; + size_t __n = __sz; + for (size_t __i = 0; __i < __sz; ++__i) + { + if (__found[__i]) + { + if (__n < __sz) // more than one _Tp found + return __sz; + __n = __i; + } + } + return __n; + } + // The standard says this macro and alias template should be in but we // we define them here, to be available in , and too. // _GLIBCXX_RESOLVE_LIB_DEFECTS diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index b82cdf12569..46173935b64 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -1419,28 +1419,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #define __cpp_lib_tuples_by_type 201304L - // Return the index of _Tp in _Types, if it occurs exactly once. - // Otherwise, return sizeof...(_Types). - // TODO reuse this for __detail::__variant::__exactly_once. - template - constexpr size_t - __find_uniq_type_in_pack() - { - constexpr size_t __sz = sizeof...(_Types); - constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... }; - size_t __n = __sz; - for (size_t __i = 0; __i < __sz; ++__i) - { - if (__found[__i]) - { - if (__n < __sz) // more than one _Tp found - return __sz; - __n = __i; - } - } - return __n; - } - /// Return a reference to the unique element of type _Tp of a tuple. template constexpr _Tp& diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 993ce3dba91..ab4503bc7c1 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -161,19 +161,6 @@ namespace __detail { namespace __variant { - // Returns the first appearance of _Tp in _Types. - // Returns sizeof...(_Types) if _Tp is not in _Types. - template - struct __index_of : std::integral_constant {}; - - template - inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value; - - template - struct __index_of<_Tp, _First, _Rest...> : - std::integral_constant - ? 0 : __index_of_v<_Tp, _Rest...> + 1> {}; - // used for raw visitation struct __variant_cookie {}; // used for raw visitation with indices passed in @@ -766,21 +753,9 @@ namespace __variant _Variant_base& operator=(_Variant_base&&) = default; }; - // How many times does _Tp appear in _Types? template - inline constexpr size_t __count = 0; - - template - inline constexpr size_t __count<_Tp, _Up, _Types...> - = __count<_Tp, _Types...>; - - template - inline constexpr size_t __count<_Tp, _Tp, _Types...> - = 1 + __count<_Tp, _Types...>; - - // TODO: Reuse this in ? - template - inline constexpr bool __exactly_once = __count<_Tp, _Types...> == 1; + inline constexpr bool __exactly_once + = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types); // Helper used to check for valid conversions that don't involve narrowing. template struct _Arr { _Ti _M_x[1]; }; @@ -1139,45 +1114,51 @@ namespace __variant { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); - return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>; + return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>(); } template - constexpr _Tp& get(variant<_Types...>& __v) + constexpr _Tp& + get(variant<_Types...>& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(__v); } template - constexpr _Tp&& get(variant<_Types...>&& __v) + constexpr _Tp&& + get(variant<_Types...>&& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>( - std::move(__v)); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(std::move(__v)); } template - constexpr const _Tp& get(const variant<_Types...>& __v) + constexpr const _Tp& + get(const variant<_Types...>& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(__v); } template - constexpr const _Tp&& get(const variant<_Types...>&& __v) + constexpr const _Tp&& + get(const variant<_Types...>&& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>( - std::move(__v)); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(std::move(__v)); } template @@ -1214,8 +1195,8 @@ namespace __variant static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>( - __ptr); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get_if<__n>(__ptr); } template @@ -1225,8 +1206,8 @@ namespace __variant static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>( - __ptr); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get_if<__n>(__ptr); } struct monostate { }; @@ -1402,8 +1383,8 @@ namespace __variant using __accepted_type = __to_type<__accepted_index<_Tp>>; template - static constexpr size_t __index_of = - __detail::__variant::__index_of_v<_Tp, _Types...>; + static constexpr size_t __index_of + = std::__find_uniq_type_in_pack<_Tp, _Types...>(); using _Traits = __detail::__variant::_Traits<_Types...>;