From patchwork Mon Oct 10 15:34:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 680454 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 3st41x4Hrwz9rvt for ; Tue, 11 Oct 2016 02:34:59 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=WcHUNMWT; 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=t9EIJvOvdQW3slUzpqhBZ13UPOoShOhptTgfZcjKXLnEiTrNpN1/F qyub98yJ3132Dg4HdKQR31DFLUkFHsr32yuUwyEnbFliP+6/jSTYQ6RjR1XW19BQ gbCw/jqdRleea4fu8XOUz4p6uMSKTYyMNlBnWjgoXheagKhtvb6CEs= 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=LsjrZeFzjaDlXGdK2IZYpPbPHvI=; b=WcHUNMWTyf6g8DURg4Ry oIcEdUk7HNHMMXR/b44L9sYz6UBzWXrR/XkflWAQB9UBj6MEbaN2TmyoKWDWM3kB rmwelCJ8M42buB1atcWzXd96qF952rvmXBQvI+lxDMr/6g6Q6KHB7arjWIVhGJgB tZ1u5LSVl8wpnQCSFf87g3g= Received: (qmail 2281 invoked by alias); 10 Oct 2016 15:34:44 -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 2260 invoked by uid 89); 10 Oct 2016 15:34:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=para 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 ESMTP; Mon, 10 Oct 2016 15:34:33 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (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 9CAA931B309; Mon, 10 Oct 2016 15:34:32 +0000 (UTC) Received: from localhost (ovpn-116-39.ams2.redhat.com [10.36.116.39]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u9AFYVYa002476; Mon, 10 Oct 2016 11:34:32 -0400 Date: Mon, 10 Oct 2016 16:34:31 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Implement constexpr std::addressof for C++17 Message-ID: <20161010153431.GA6053@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.0 (2016-08-17) Thank to the new __builtin_addressof that Jakub added we can do this now. * doc/xml/manual/intro.xml: Document DR 2296 status. * doc/xml/manual/status_cxx2017.xml: Update status. * include/bits/move.h (__addressof): Add _GLIBCXX_CONSTEXPR and call __builtin_addressof. (addressof): Add _GLIBCXX17_CONSTEXPR. * testsuite/20_util/addressof/requirements/constexpr.cc: New test. * testsuite/20_util/forward/c_neg.cc: Adjust dg-error lineno. * testsuite/20_util/forward/f_neg.cc: Likewise. Tested powerpc64le-linux, committed to trunk. commit d450a86bff30b38464e440f6157c39b399b54cc1 Author: Jonathan Wakely Date: Thu Oct 6 18:53:28 2016 +0100 Implement constexpr std::addressof for C++17 * doc/xml/manual/intro.xml: Document DR 2296 status. * doc/xml/manual/status_cxx2017.xml: Update status. * include/bits/move.h (__addressof): Add _GLIBCXX_CONSTEXPR and call __builtin_addressof. (addressof): Add _GLIBCXX17_CONSTEXPR. * testsuite/20_util/addressof/requirements/constexpr.cc: New test. * testsuite/20_util/forward/c_neg.cc: Adjust dg-error lineno. * testsuite/20_util/forward/f_neg.cc: Likewise. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 4747851..265ef67 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -961,6 +961,13 @@ requirements of the license of GCC. is included by <array>. + 2296: + std::addressof should be constexpr + + Use __builtin_addressof and add + constexpr to addressof for C++17 and later. + + 2313: tuple_size should always derive from integral_constant<size_t, N> diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index c03978e..c6b8440 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml @@ -253,14 +253,13 @@ Feature-testing recommendations for C++. - std::addressof should be constexpr LWG2296 - No + 7 __cpp_lib_addressof_constexpr >= 201603 diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h index 9deec42..a5002fc 100644 --- a/libstdc++-v3/include/bits/move.h +++ b/libstdc++-v3/include/bits/move.h @@ -43,12 +43,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @ingroup utilities */ template - inline _Tp* + inline _GLIBCXX_CONSTEXPR _Tp* __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT - { - return reinterpret_cast<_Tp*> - (&const_cast(reinterpret_cast(__r))); - } + { return __builtin_addressof(__r); } _GLIBCXX_END_NAMESPACE_VERSION } // namespace @@ -123,6 +120,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // declval, from type_traits. +#if __cplusplus > 201402L + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2296. std::addressof should be constexpr +# define __cpp_lib_addressof_constexpr 201603 +#endif /** * @brief Returns the actual address of the object or function * referenced by r, even in the presence of an overloaded @@ -131,7 +133,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @return The actual address. */ template - inline _Tp* + inline _GLIBCXX17_CONSTEXPR _Tp* addressof(_Tp& __r) noexcept { return std::__addressof(__r); } diff --git a/libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc b/libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc new file mode 100644 index 0000000..998d087 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc @@ -0,0 +1,55 @@ +// 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++1z" } +// { dg-do compile { target c++1z } } + +#include + +// LWG 2296 std::addressof should be constexpr + +#ifndef __cpp_lib_addressof_constexpr +# error "Feature-test macro for constexpr addressof missing" +#elif __cpp_lib_addressof_constexpr != 201603 +# error "Feature-test macro for constexpr addressof has wrong value" +#endif + +int i; +constexpr int* pi = std::addressof(i); +static_assert( pi == &i, "&i" ); + +struct X { + void operator&(); + constexpr X* addr() { return this; } + constexpr const X* addr() const { return this; } +}; +X x; +constexpr X* px = std::addressof(x); +static_assert( px == x.addr(), "x.addr()" ); +const X cx; +constexpr const X* pcx = std::addressof(cx); +static_assert( pcx == cx.addr(), "cx.addr()" ); + +void +test01() +{ + static int i2; + static_assert( std::addressof(i2) == &i2, "&i2" ); + + static X x2; + static_assert( std::addressof(x2) == x2.addr(), "x2.addr()" ); +} diff --git a/libstdc++-v3/testsuite/20_util/forward/c_neg.cc b/libstdc++-v3/testsuite/20_util/forward/c_neg.cc index 0b23625..784e23c 100644 --- a/libstdc++-v3/testsuite/20_util/forward/c_neg.cc +++ b/libstdc++-v3/testsuite/20_util/forward/c_neg.cc @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// { dg-error "static assertion failed" "" { target *-*-* } 89 } +// { dg-error "static assertion failed" "" { target *-*-* } 86 } #include diff --git a/libstdc++-v3/testsuite/20_util/forward/f_neg.cc b/libstdc++-v3/testsuite/20_util/forward/f_neg.cc index 9796ef4..3f4c175 100644 --- a/libstdc++-v3/testsuite/20_util/forward/f_neg.cc +++ b/libstdc++-v3/testsuite/20_util/forward/f_neg.cc @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// { dg-error "static assertion failed" "" { target *-*-* } 89 } +// { dg-error "static assertion failed" "" { target *-*-* } 86 } #include