From patchwork Thu Nov 4 09:42:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 47038 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9FC043858039 for ; Thu, 4 Nov 2021 09:44:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9FC043858039 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636019094; bh=90l6DmFugnqb9STXuVhh7/saviehwXL3S3xdX2j7lEg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=xuLIAgv2CPE++EUQ/ofbsSSzW72nZZdEOMjb5/Y1mCkNi097AeEa1NI4amgEvQLRQ 3bpQcNBpi5r0o+R9IR/309IHxRLDKPbThbtD4IuLmMCaZHLR9/sSo6L40uP5Gxlci1 spnCu65aaAYicFtoyQsa9uwT8IF8DI0IetKiWpQo= 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 ESMTPS id A29403858C3A for ; Thu, 4 Nov 2021 09:42:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A29403858C3A 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-293-dN9dcm8KNN6oky0B9z8ieQ-1; Thu, 04 Nov 2021 05:42:13 -0400 X-MC-Unique: dN9dcm8KNN6oky0B9z8ieQ-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 3F2CC80A5C2; Thu, 4 Nov 2021 09:42:12 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB41B5D9D3; Thu, 4 Nov 2021 09:42:11 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/3] libstdc++: Optimize std::variant traits and improve diagnostics Date: Thu, 4 Nov 2021 09:42:08 +0000 Message-Id: <20211104094209.1930691-2-jwakely@redhat.com> In-Reply-To: <20211104094209.1930691-1-jwakely@redhat.com> References: <20211104094209.1930691-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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. By defining additional partial specializations of _Nth_type we can reduce the number of recursive instantiations needed to get from N to 0. We can also use _Nth_type in variant_alternative, to take advantage of that new optimization. By adding a static_assert to variant_alternative we get a nicer error than 'invalid use of incomplete type'. By defining partial specializations of std::variant_size_v for the common case we can avoid instantiating the std::variant_size class template. The __tuple_count class template and __tuple_count_v variable template can be simplified to a single variable template, __count. By adding a deleted constructor to the _Variant_union primary template we can (very slightly) improve diagnostics for invalid attempts to construct a std::variant with an out-of-range index. Instead of a confusing error about "too many initializers for ..." we get a call to a deleted function. By using _Nth_type instead of variant_alternative (for cv-unqualified variant types) we avoid instantiating variant_alternative. By adding deleted overloads of variant::emplace we get better diagnostics for emplace or emplace. Instead of getting errors explaining why each of the four overloads wasn't valid, we just get one error about calling a deleted function. libstdc++-v3/ChangeLog: * include/std/variant (_Nth_type): Define partial specializations to reduce number of instantiations. (variant_size_v): Define partial specializations to avoid instantiations. (variant_alternative): Use _Nth_type. Add static assert. (__tuple_count, __tuple_count_v): Replace with ... (__count): New variable template. (_Variant_union): Add deleted constructor. (variant::__to_type): Use _Nth_type. (variant::emplace): Use _Nth_type. Add deleted overloads for invalid types and indices. --- libstdc++-v3/include/std/variant | 119 ++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 41 deletions(-) diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 63468af7012..dc3d032c543 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -61,13 +61,40 @@ namespace __variant template struct _Nth_type; - template - struct _Nth_type<_Np, _First, _Rest...> - : _Nth_type<_Np-1, _Rest...> { }; + template + struct _Nth_type<0, _Tp0, _Rest...> + { using type = _Tp0; }; - template - struct _Nth_type<0, _First, _Rest...> - { using type = _First; }; + template + struct _Nth_type<1, _Tp0, _Tp1, _Rest...> + { using type = _Tp1; }; + + template + struct _Nth_type<2, _Tp0, _Tp1, _Tp2, _Rest...> + { using type = _Tp2; }; + + template +#if __cpp_concepts + requires (_Np >= 3) +#endif + struct _Nth_type<_Np, _Tp0, _Tp1, _Tp2, _Rest...> + : _Nth_type<_Np - 3, _Rest...> + { }; + +#if ! __cpp_concepts // Need additional specializations to avoid ambiguities. + template + struct _Nth_type<0, _Tp0, _Tp1, _Rest...> + { using type = _Tp0; }; + + template + struct _Nth_type<0, _Tp0, _Tp1, _Tp2, _Rest...> + { using type = _Tp0; }; + + template + struct _Nth_type<1, _Tp0, _Tp1, _Tp2, _Rest...> + { using type = _Tp1; }; +#endif } // namespace __variant } // namespace __detail @@ -102,16 +129,25 @@ namespace __variant template inline constexpr size_t variant_size_v = variant_size<_Variant>::value; + template + inline constexpr size_t + variant_size_v> = sizeof...(_Types); + + template + inline constexpr size_t + variant_size_v> = sizeof...(_Types); + template struct variant_alternative; - template - struct variant_alternative<_Np, variant<_First, _Rest...>> - : variant_alternative<_Np-1, variant<_Rest...>> {}; + template + struct variant_alternative<_Np, variant<_Types...>> + { + static_assert(_Np < sizeof...(_Types)); - template - struct variant_alternative<0, variant<_First, _Rest...>> - { using type = _First; }; + using type + = typename __detail::__variant::_Nth_type<_Np, _Types...>::type; + }; template using variant_alternative_t = @@ -390,7 +426,13 @@ namespace __variant // Defines members and ctors. template - union _Variadic_union { }; + union _Variadic_union + { + _Variadic_union() = default; + + template + _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete; + }; template union _Variadic_union<_First, _Rest...> @@ -758,28 +800,21 @@ namespace __variant _Variant_base& operator=(_Variant_base&&) = default; }; - // For how many times does _Tp appear in _Tuple? - template - struct __tuple_count; + // How many times does _Tp appear in _Types? + template + inline constexpr size_t __count = 0; - template - inline constexpr size_t __tuple_count_v = - __tuple_count<_Tp, _Tuple>::value; + template + inline constexpr size_t __count<_Tp, _Up, _Types...> + = __count<_Tp, _Types...>; template - struct __tuple_count<_Tp, tuple<_Types...>> - : integral_constant { }; - - template - struct __tuple_count<_Tp, tuple<_First, _Rest...>> - : integral_constant< - size_t, - __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { }; + inline constexpr size_t __count<_Tp, _Tp, _Types...> + = 1 + __count<_Tp, _Types...>; // TODO: Reuse this in ? template - inline constexpr bool __exactly_once = - __tuple_count_v<_Tp, tuple<_Types...>> == 1; + inline constexpr bool __exactly_once = __count<_Tp, _Types...> == 1; // Helper used to check for valid conversions that don't involve narrowing. template struct _Arr { _Ti _M_x[1]; }; @@ -1411,7 +1446,8 @@ namespace __variant = __detail::__variant::__accepted_index<_Tp, variant>::value; template> - using __to_type = variant_alternative_t<_Np, variant>; + using __to_type + = typename __detail::__variant::_Nth_type<_Np, _Types...>::type; template>> using __accepted_type = __to_type<__accepted_index<_Tp>>; @@ -1543,15 +1579,12 @@ namespace __variant template _GLIBCXX20_CONSTEXPR - enable_if_t, - _Args...>, - variant_alternative_t<_Np, variant>&> + enable_if_t, _Args...>, + __to_type<_Np>&> emplace(_Args&&... __args) { - static_assert(_Np < sizeof...(_Types), - "The index must be in [0, number of alternatives)"); - using type = variant_alternative_t<_Np, variant>; namespace __variant = std::__detail::__variant; + using type = typename __variant::_Nth_type<_Np, _Types...>::type; // Provide the strong exception-safety guarantee when possible, // to avoid becoming valueless. if constexpr (is_nothrow_constructible_v) @@ -1590,15 +1623,13 @@ namespace __variant template _GLIBCXX20_CONSTEXPR - enable_if_t, + enable_if_t, initializer_list<_Up>&, _Args...>, - variant_alternative_t<_Np, variant>&> + __to_type<_Np>&> emplace(initializer_list<_Up> __il, _Args&&... __args) { - static_assert(_Np < sizeof...(_Types), - "The index must be in [0, number of alternatives)"); - using type = variant_alternative_t<_Np, variant>; namespace __variant = std::__detail::__variant; + using type = typename __variant::_Nth_type<_Np, _Types...>::type; // Provide the strong exception-safety guarantee when possible, // to avoid becoming valueless. if constexpr (is_nothrow_constructible_v(*this); } + template + enable_if_t emplace(_Args&&...) = delete; + + template + enable_if_t> emplace(_Args&&...) = delete; + constexpr bool valueless_by_exception() const noexcept { return !this->_M_valid(); }