From patchwork Wed Aug 3 18:13:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 655552 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3s4Lm36WZfz9t0P for ; Thu, 4 Aug 2016 04:13:23 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=Y+42qmre; dkim-atps=neutral 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=y1Zce2/frOykuQoCbfvtQhSIfV76fgSGcz6wfYObJIgF0L91hUWmR gTCDd3y2T5cB0rIUW1FJ0V37IeoLIqNeYqizpEr2MHfGtiCx3OM1mltfOA0Za9f5 IAm4XHs9Zfsf191ibfhW+8/zkIri3279ANWQMn9ULSrA20XhRFNBaA= 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=NAaXRN89ofsOjZDZ2JZMRbiOkNk=; b=Y+42qmreB9aogPBgiOzN U6N645TC3g6sk3yRGvp3LN48IGg19dqjOaG21kaIsr5NEQEXeGkIWRvQ41xyaKyF wZfJv7DCEEcop3vpGvZQjq3keBAMX24oiy/N9Jo/EgoCGEj/0Kr4+ekvGWsShRXt CvJn/iSCLGm++zuxVyrfLPM= Received: (qmail 40784 invoked by alias); 3 Aug 2016 18:13:14 -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 40766 invoked by uid 89); 3 Aug 2016 18:13:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.1 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=1, 28 X-Spam-User: qpsmtpd, 2 recipients 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 03 Aug 2016 18:13:03 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2F029C057FAD; Wed, 3 Aug 2016 18:13:02 +0000 (UTC) Received: from localhost (ovpn-116-93.ams2.redhat.com [10.36.116.93]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u73ID1Iw031140; Wed, 3 Aug 2016 14:13:01 -0400 Date: Wed, 3 Aug 2016 19:13:00 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Define std::as_const Message-ID: <20160803181300.GA28780@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.6.2 (2016-07-01) Another C++17 feature. * include/std/utility (as_const): Define. * testsuite/20_util/as_const/1.cc: New test. * testsuite/20_util/as_const/rvalue_neg.cc: New test. Tested powerpc64-linux, committed to trunk. commit c6d91a7d6d5ec64f8c4e84cd79aadde72f01a4f4 Author: Jonathan Wakely Date: Wed Aug 3 18:43:20 2016 +0100 Define std::as_const * include/std/utility (as_const): Define. * testsuite/20_util/as_const/1.cc: New test. * testsuite/20_util/as_const/rvalue_neg.cc: New test. diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index 106ba4d..0c03644 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -356,6 +356,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template in_place_tag in_place(__in_place_index<_Idx>*) {terminate();} +#define __cpp_lib_as_const 201510 + template + constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } + + template + void as_const(const _Tp&&) = delete; + #endif _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/testsuite/20_util/as_const/1.cc b/libstdc++-v3/testsuite/20_util/as_const/1.cc new file mode 100644 index 0000000..2f257b4 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/as_const/1.cc @@ -0,0 +1,30 @@ +// Copyright (C) 2016 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++17" } +// { dg-do compile } + +#include + +#if __cpp_lib_as_const != 201510 +# error "__cpp_lib_as_const != 201510" +#endif + +int i; +constexpr auto& ci = std::as_const(i); +static_assert( &i == &ci ); +static_assert( std::is_same_v ); diff --git a/libstdc++-v3/testsuite/20_util/as_const/rvalue_neg.cc b/libstdc++-v3/testsuite/20_util/as_const/rvalue_neg.cc new file mode 100644 index 0000000..3fe9e18 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/as_const/rvalue_neg.cc @@ -0,0 +1,28 @@ +// Copyright (C) 2016 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++17" } +// { dg-do compile } + +#include + +void test01() +{ + int i = 0; + std::as_const(std::move(i)); // { dg-error "deleted function" } + std::as_const(0); // { dg-error "deleted function" } +}