/* Copyright (C) 2002 Brad King (brad.king@kitware.com) Douglas Gregor (gregod@cs.rpi.edu) Copyright (C) 2002, 2008, 2013 Peter Dimov Copyright (C) 2017 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ #ifndef BHO_CORE_ADDRESSOF_HPP #define BHO_CORE_ADDRESSOF_HPP #include #if defined(BHO_MSVC_FULL_VER) && BHO_MSVC_FULL_VER >= 190024215 #define BHO_CORE_HAS_BUILTIN_ADDRESSOF #elif defined(BHO_GCC) && BHO_GCC >= 70000 #define BHO_CORE_HAS_BUILTIN_ADDRESSOF #elif defined(__has_builtin) #if __has_builtin(__builtin_addressof) #define BHO_CORE_HAS_BUILTIN_ADDRESSOF #endif #endif #if defined(BHO_CORE_HAS_BUILTIN_ADDRESSOF) #if defined(BHO_NO_CXX11_CONSTEXPR) #define BHO_CORE_NO_CONSTEXPR_ADDRESSOF #endif namespace bho { template BHO_CONSTEXPR inline T* addressof(T& o) BHO_NOEXCEPT { return __builtin_addressof(o); } } /* boost */ #else #include #include namespace bho { namespace detail { template class addrof_ref { public: BHO_FORCEINLINE addrof_ref(T& o) BHO_NOEXCEPT : o_(o) { } BHO_FORCEINLINE operator T&() const BHO_NOEXCEPT { return o_; } private: addrof_ref& operator=(const addrof_ref&); T& o_; }; template struct addrof { static BHO_FORCEINLINE T* get(T& o, long) BHO_NOEXCEPT { return reinterpret_cast(& const_cast(reinterpret_cast(o))); } static BHO_FORCEINLINE T* get(T* p, int) BHO_NOEXCEPT { return p; } }; #if !defined(BHO_NO_CXX11_NULLPTR) #if !defined(BHO_NO_CXX11_DECLTYPE) && \ (defined(__INTEL_COMPILER) || \ (defined(__clang__) && !defined(_LIBCPP_VERSION))) typedef decltype(nullptr) addrof_null_t; #else typedef std::nullptr_t addrof_null_t; #endif template<> struct addrof { typedef addrof_null_t type; static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT { return &o; } }; template<> struct addrof { typedef const addrof_null_t type; static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT { return &o; } }; template<> struct addrof { typedef volatile addrof_null_t type; static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT { return &o; } }; template<> struct addrof { typedef const volatile addrof_null_t type; static BHO_FORCEINLINE type* get(type& o, int) BHO_NOEXCEPT { return &o; } }; #endif } /* detail */ #if defined(BHO_NO_CXX11_SFINAE_EXPR) || \ defined(BHO_NO_CXX11_CONSTEXPR) || \ defined(BHO_NO_CXX11_DECLTYPE) #define BHO_CORE_NO_CONSTEXPR_ADDRESSOF template BHO_FORCEINLINE T* addressof(T& o) BHO_NOEXCEPT { #if BHO_WORKAROUND(BHO_BORLANDC, BHO_TESTED_AT(0x610)) || \ BHO_WORKAROUND(__SUNPRO_CC, <= 0x5120) return bho::detail::addrof::get(o, 0); #else return bho::detail::addrof::get(bho::detail::addrof_ref(o), 0); #endif } #if BHO_WORKAROUND(__SUNPRO_CC, BHO_TESTED_AT(0x590)) namespace detail { template struct addrof_result { typedef T* type; }; } /* detail */ template BHO_FORCEINLINE typename bho::detail::addrof_result::type addressof(T (&o)[N]) BHO_NOEXCEPT { return &o; } #endif #if BHO_WORKAROUND(BHO_BORLANDC, BHO_TESTED_AT(0x564)) template BHO_FORCEINLINE T (*addressof(T (&o)[N]) BHO_NOEXCEPT)[N] { return reinterpret_cast(&o); } template BHO_FORCEINLINE const T (*addressof(const T (&o)[N]) BHO_NOEXCEPT)[N] { return reinterpret_cast(&o); } #endif #else namespace detail { template T addrof_declval() BHO_NOEXCEPT; template struct addrof_void { typedef void type; }; template struct addrof_member_operator { static constexpr bool value = false; }; template struct addrof_member_operator().operator&())>::type> { static constexpr bool value = true; }; #if BHO_WORKAROUND(BHO_INTEL, < 1600) struct addrof_addressable { }; addrof_addressable* operator&(addrof_addressable&) BHO_NOEXCEPT; #endif template struct addrof_non_member_operator { static constexpr bool value = false; }; template struct addrof_non_member_operator()))>::type> { static constexpr bool value = true; }; template struct addrof_expression { static constexpr bool value = false; }; template struct addrof_expression())>::type> { static constexpr bool value = true; }; template struct addrof_is_constexpr { static constexpr bool value = addrof_expression::value && !addrof_member_operator::value && !addrof_non_member_operator::value; }; template struct addrof_if { }; template struct addrof_if { typedef T* type; }; template BHO_FORCEINLINE typename addrof_if::value, T>::type addressof(T& o) BHO_NOEXCEPT { return addrof::get(addrof_ref(o), 0); } template constexpr BHO_FORCEINLINE typename addrof_if::value, T>::type addressof(T& o) BHO_NOEXCEPT { return &o; } } /* detail */ template constexpr BHO_FORCEINLINE T* addressof(T& o) BHO_NOEXCEPT { return bho::detail::addressof(o); } #endif } /* boost */ #endif #if !defined(BHO_NO_CXX11_RVALUE_REFERENCES) && \ !defined(BHO_NO_CXX11_DELETED_FUNCTIONS) namespace bho { template const T* addressof(const T&&) = delete; } /* boost */ #endif #endif