From 9e023e3321886e0ba1ea7b25c7ef70e50c267963 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 10 Oct 2016 16:34:28 +0100 Subject: [PATCH] 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. From-SVN: r240929 --- libstdc++-v3/ChangeLog | 9 +++ libstdc++-v3/doc/xml/manual/intro.xml | 7 +++ .../doc/xml/manual/status_cxx2017.xml | 3 +- libstdc++-v3/include/bits/move.h | 14 +++-- .../addressof/requirements/constexpr.cc | 55 +++++++++++++++++++ .../testsuite/20_util/forward/c_neg.cc | 2 +- .../testsuite/20_util/forward/f_neg.cc | 2 +- 7 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d1e2bc37523..34b7d05f3b4 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,14 @@ 2016-10-10 Jonathan Wakely + * 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. + * include/bits/allocator.h (allocator::is_always_equal): Define. * testsuite/20_util/allocator/requirements/typedefs.cc: Test for is_always_equal. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 47478513666..265ef67aaae 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 c03978e5e1f..c6b84409f68 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 9deec427e24..a5002fca422 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 00000000000..998d0877cd4 --- /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 0b23625e47c..784e23c31e7 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 9796ef4399b..3f4c175bc6b 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