From e6e6a04114997cc96a3c71d18933d5e19f2875e0 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Fri, 6 Jan 2017 16:35:57 +0300 Subject: [PATCH] Changed taking address of the values involved in bitwise_cast to avoid gcc warnings. --- include/boost/atomic/detail/bitwise_cast.hpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/include/boost/atomic/detail/bitwise_cast.hpp b/include/boost/atomic/detail/bitwise_cast.hpp index 8654d10b..1405a25d 100644 --- a/include/boost/atomic/detail/bitwise_cast.hpp +++ b/include/boost/atomic/detail/bitwise_cast.hpp @@ -29,6 +29,17 @@ namespace boost { namespace atomics { namespace detail { +template< typename T > +BOOST_FORCEINLINE T* addressof(T& value) BOOST_NOEXCEPT +{ + // Note: The point of using a local struct as the intermediate type instead of char is to avoid gcc warnings + // if T is a const volatile char*: + // warning: casting ‘const volatile char* const’ to ‘const volatile char&’ does not dereference pointer + // The local struct makes sure T is not related to the cast target type. + struct opaque_type; + return reinterpret_cast< T* >(&const_cast< opaque_type& >(reinterpret_cast< const volatile opaque_type& >(value))); +} + template< typename To, typename From > BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT { @@ -39,8 +50,8 @@ BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT value = {}; BOOST_ATOMIC_DETAIL_MEMCPY ( - &reinterpret_cast< char& >(value.to), - &reinterpret_cast< const char& >(from), + atomics::detail::addressof(value.to), + atomics::detail::addressof(from), (sizeof(From) < sizeof(To) ? sizeof(From) : sizeof(To)) ); return value.to;