Skip to content

Commit

Permalink
Changed taking address of the values involved in bitwise_cast to avoi…
Browse files Browse the repository at this point in the history
…d gcc warnings.
  • Loading branch information
Lastique committed Jan 6, 2017
1 parent 41eac78 commit e6e6a04
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions include/boost/atomic/detail/bitwise_cast.hpp
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down

0 comments on commit e6e6a04

Please sign in to comment.