From patchwork Sun Nov 11 05:17:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 996056 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-489636-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="R3ddHpPf"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42t2G869M8z9sBk for ; Sun, 11 Nov 2018 16:17:52 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=HAcZl8dCQi+3CH3jSGzmcrj3Pot6FzWzD2+cpcE+3ySHT1ScgGcEO TdCj+8fgPPNa17rNW2b3K4Sc7NB7LwoX0spHkPDD2vDzCssLFVA5CUe/JIbigsto H1BpZsDOiJCVowLV552Gzv859MjglGjVGoC/4u6EtFjvYMPc8dMTVw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=thUF7j0rCcUg3TKv3lSDuItDJVA=; b=R3ddHpPf30wlaiRp0cEO ubSWejzyU7vgGw0eIltX/1gEhUANkqQukTmyfvFgIkedP4elMPal3NATnyL8Za0Z SMCC5NlO3Bd5e26qZCWO0AWBY+tVUe0Fb0N8pNnJevD0qx+UKNcpnMfkMfnnLGAs fd5wMOz7wUIOYjpRtsyaggY= Received: (qmail 81761 invoked by alias); 11 Nov 2018 05:17:17 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 81448 invoked by uid 89); 11 Nov 2018 05:17:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=2cc, 2.cc, UD:2.cc X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 11 Nov 2018 05:17:12 +0000 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 mx1.redhat.com (Postfix) with ESMTPS id DA3493082125; Sun, 11 Nov 2018 05:17:10 +0000 (UTC) Received: from localhost (ovpn-116-29.phx2.redhat.com [10.3.116.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id ABE35194A9; Sun, 11 Nov 2018 05:17:10 +0000 (UTC) Date: Sun, 11 Nov 2018 05:17:10 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Implement P0318R1 unwrap_ref_decay and unwrap_reference Message-ID: <20181111051709.GA9146@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) Implement P0318R1 unwrap_ref_decay and unwrap_reference * include/std/type_traits (unwrap_reference, unwrap_reference_t) (unwrap_ref_decay, unwrap_ref_decay_t): New traits and aliases. * testsuite/20_util/unwrap_reference/1.cc: New test. * testsuite/20_util/unwrap_reference/2.cc: New test. Tested x86_64-linux, committed to trunk. commit 5c164b36d9859e73cdac91ce551887a6c72620c7 Author: Jonathan Wakely Date: Sun Nov 11 01:25:44 2018 +0000 Implement P0318R1 unwrap_ref_decay and unwrap_reference Implement P0318R1 unwrap_ref_decay and unwrap_reference * include/std/type_traits (unwrap_reference, unwrap_reference_t) (unwrap_ref_decay, unwrap_ref_decay_t): New traits and aliases. * testsuite/20_util/unwrap_reference/1.cc: New test. * testsuite/20_util/unwrap_reference/2.cc: New test. diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 86b58ccf225..60094f9897b 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -3015,6 +3015,20 @@ template template using type_identity_t = typename type_identity<_Tp>::type; + /// Unwrap a reference_wrapper + template + struct unwrap_reference { using type = _Tp; }; + + template + struct unwrap_reference> { using type = _Tp&; }; + + /// Decay type and if it's a reference_wrapper, unwrap it + template + struct unwrap_ref_decay : unwrap_reference> { }; + + template + using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type; + #endif // C++2a _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/testsuite/20_util/unwrap_reference/1.cc b/libstdc++-v3/testsuite/20_util/unwrap_reference/1.cc new file mode 100644 index 00000000000..d259db91418 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unwrap_reference/1.cc @@ -0,0 +1,58 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template struct expect_same; +template struct expect_same : std::true_type { }; + +template + constexpr bool check() + { + using std::unwrap_reference; + using T2 = typename unwrap_reference::type; + static_assert(expect_same::type>::value); + return expect_same::value; + } + +void +test01() +{ + static_assert( check() ); + static_assert( check() ); + static_assert( check() ); + static_assert( check() ); + static_assert( check() ); + + // reference_wrapper types should get unwrapped: + static_assert( check, int&>() ); + static_assert( check, const int&>() ); + static_assert( check, long&>() ); + + // But not cv-qualified reference_wrapper types: + static_assert( check>() ); + static_assert( check>() ); + static_assert( check>() ); + + // Or references to reference_wrapper types: + static_assert( check&>() ); + static_assert( check&&>() ); + static_assert( check&>() ); +} diff --git a/libstdc++-v3/testsuite/20_util/unwrap_reference/2.cc b/libstdc++-v3/testsuite/20_util/unwrap_reference/2.cc new file mode 100644 index 00000000000..47bfa71975e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unwrap_reference/2.cc @@ -0,0 +1,51 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +template struct expect_same; +template struct expect_same : std::true_type { }; + +template> + constexpr bool check() + { + using T2 = typename std::unwrap_ref_decay::type; + static_assert(expect_same>::value); + return expect_same::value; + } + +void +test01() +{ + static_assert( check() ); + static_assert( check() ); + static_assert( check() ); + static_assert( check() ); + static_assert( check() ); + + // reference_wrapper types (including cv and references) get unwrapped: + static_assert( check, int&>() ); + static_assert( check&, int&>() ); + static_assert( check, int&>() ); + static_assert( check&, int&>() ); + static_assert( check, const int&>() ); + static_assert( check&, const int&>() ); + static_assert( check, long&>() ); +}