From 735026e3eeb9cd6aa018db7f96ea47338c628365 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Thu, 17 Mar 2016 03:30:56 +0000 Subject: [PATCH] Make std::addressof constexpr in C++17 (Clang only). git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@263688 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/__config | 6 +++ include/type_traits | 14 +++++++ .../constexpr_addressof.pass.cpp | 42 +++++++++++++++++++ www/cxx1z_status.html | 2 +- 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/std/utilities/memory/specialized.algorithms/specialized.addressof/constexpr_addressof.pass.cpp diff --git a/include/__config b/include/__config index c360214b9..f6047cac2 100644 --- a/include/__config +++ b/include/__config @@ -779,6 +779,12 @@ template struct __static_assert_check {}; #define _LIBCPP_CONSTEXPR_AFTER_CXX11 #endif +#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_CXX14_CONSTEXPR) +#define _LIBCPP_CONSTEXPR_AFTER_CXX14 constexpr +#else +#define _LIBCPP_CONSTEXPR_AFTER_CXX14 +#endif + #ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES # define _LIBCPP_EXPLICIT_MOVE(x) _VSTD::move(x) #else diff --git a/include/type_traits b/include/type_traits index 29228395f..6840ee23a 100644 --- a/include/type_traits +++ b/include/type_traits @@ -397,6 +397,18 @@ template using enable_if_t = typename enable_if<_Bp #endif // addressof +#if __has_builtin(__builtin_addressof) + +template +inline _LIBCPP_CONSTEXPR_AFTER_CXX14 +_LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY +_Tp* +addressof(_Tp& __x) _NOEXCEPT +{ + return __builtin_addressof(__x); +} + +#else template inline _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY @@ -406,6 +418,8 @@ addressof(_Tp& __x) _NOEXCEPT return (_Tp*)&reinterpret_cast(__x); } +#endif // __has_builtin(__builtin_addressof) + #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF) // Objective-C++ Automatic Reference Counting uses qualified pointers // that require special addressof() signatures. When diff --git a/test/std/utilities/memory/specialized.algorithms/specialized.addressof/constexpr_addressof.pass.cpp b/test/std/utilities/memory/specialized.algorithms/specialized.addressof/constexpr_addressof.pass.cpp new file mode 100644 index 000000000..a371f8eda --- /dev/null +++ b/test/std/utilities/memory/specialized.algorithms/specialized.addressof/constexpr_addressof.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// XFAIL: gcc + +// + +// template constexpr T* addressof(T& r); + +#include +#include + +struct Pointer { + constexpr Pointer(void* v) : value(v) {} + void* value; +}; + +struct A +{ + constexpr A() : n(42) {} + void operator&() const { } + int n; +}; + +constexpr int i = 0; +constexpr double d = 0.0; +constexpr A a{}; + +int main() +{ + static_assert(std::addressof(i) == &i, ""); + static_assert(std::addressof(d) == &d, ""); + constexpr const A* ap = std::addressof(a); + static_assert(&ap->n == &a.n, ""); +} diff --git a/www/cxx1z_status.html b/www/cxx1z_status.html index 0b9023b7e..65c33c094 100644 --- a/www/cxx1z_status.html +++ b/www/cxx1z_status.html @@ -204,7 +204,7 @@ 2192Validity and return type of std::abs(0u) is unclearJacksonville 2276Missing requirement on std::promise::set_exceptionJacksonville - 2296std::addressof should be constexprJacksonville + 2296std::addressof should be constexprJacksonvilleComplete (Clang Only) 2450(greater|less|greater_equal|less_equal)<void> do not yield a total order for pointersJacksonville 2520N4089 broke initializing unique_ptr<T[]> from a nullptrJacksonville 2522[fund.ts.v2] Contradiction in set_default_resource specificationJacksonville