diff --git a/doc/move.qbk b/doc/move.qbk index e83f0b7..f85b6ce 100644 --- a/doc/move.qbk +++ b/doc/move.qbk @@ -804,6 +804,7 @@ Special thanks to: [section:release_notes_boost_1_79 Boost 1.79 Release] +* The library now compiles without warnings with GCC's -Wcast-align=strict * Fixed bugs: * [@https://github.com/boostorg/move/pull/46 Git Issue #46: ['"Include when BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE is defined"]]. * [@https://github.com/boostorg/move/issues/47 Git Issue #47: ['"MSVC C5243: 'member_function_ptr': using incomplete class can cause potential ODR violation ..."]]. diff --git a/include/boost/move/detail/addressof.hpp b/include/boost/move/detail/addressof.hpp new file mode 100644 index 0000000..b425e6c --- /dev/null +++ b/include/boost/move/detail/addressof.hpp @@ -0,0 +1,61 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2015. 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) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_MOVE_DETAIL_ADDRESSOF_HPP +#define BOOST_MOVE_DETAIL_ADDRESSOF_HPP + +#ifndef BOOST_CONFIG_HPP +# include +#endif + +#if defined(BOOST_HAS_PRAGMA_ONCE) +# pragma once +#endif + +#include + +namespace boost { +namespace move_detail { + +#if defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 190024215 +#define BOOST_MOVE_HAS_BUILTIN_ADDRESSOF +#elif defined(BOOST_GCC) && BOOST_GCC >= 70000 +#define BOOST_MOVE_HAS_BUILTIN_ADDRESSOF +#elif defined(__has_builtin) +#if __has_builtin(__builtin_addressof) +#define BOOST_MOVE_HAS_BUILTIN_ADDRESSOF +#endif +#endif + +#ifdef BOOST_MOVE_HAS_BUILTIN_ADDRESSOF + +template +BOOST_MOVE_FORCEINLINE T *addressof( T & v ) BOOST_NOEXCEPT +{ + return __builtin_addressof(v); +} + +#else //BOOST_MOVE_HAS_BUILTIN_ADDRESSOF + +template +BOOST_MOVE_FORCEINLINE T* addressof(T& obj) +{ + return static_cast( + static_cast( + const_cast( + &reinterpret_cast(obj) + ))); +} + +#endif //BOOST_MOVE_HAS_BUILTIN_ADDRESSOF + +} //namespace move_detail { +} //namespace boost { + +#endif //#ifndef BOOST_MOVE_DETAIL_ADDRESSOF_HPP diff --git a/include/boost/move/detail/force_ptr.hpp b/include/boost/move/detail/force_ptr.hpp new file mode 100644 index 0000000..f9cfacc --- /dev/null +++ b/include/boost/move/detail/force_ptr.hpp @@ -0,0 +1,36 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2015. 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) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_MOVE_DETAIL_FORCE_CAST_HPP +#define BOOST_MOVE_DETAIL_FORCE_CAST_HPP + +#ifndef BOOST_CONFIG_HPP +# include +#endif + +#if defined(BOOST_HAS_PRAGMA_ONCE) +# pragma once +#endif + +#include + +namespace boost { +namespace move_detail { + + +template +BOOST_MOVE_FORCEINLINE T force_ptr(const volatile void *p) +{ + return static_cast(const_cast(p)); +} + +} //namespace move_detail { +} //namespace boost { + +#endif //#ifndef BOOST_MOVE_DETAIL_FORCE_CAST_HPP diff --git a/include/boost/move/detail/meta_utils.hpp b/include/boost/move/detail/meta_utils.hpp index f16e185..6ee04da 100644 --- a/include/boost/move/detail/meta_utils.hpp +++ b/include/boost/move/detail/meta_utils.hpp @@ -21,6 +21,7 @@ #include //forceinline #include #include //for std::size_t +#include //Small meta-typetraits to support move @@ -241,36 +242,7 @@ struct is_class_or_union ////////////////////////////////////// // addressof ////////////////////////////////////// -template -struct addr_impl_ref -{ - T & v_; - BOOST_MOVE_FORCEINLINE addr_impl_ref( T & v ): v_( v ) {} - BOOST_MOVE_FORCEINLINE operator T& () const { return v_; } - private: - addr_impl_ref & operator=(const addr_impl_ref &); -}; - -template -struct addressof_impl -{ - BOOST_MOVE_FORCEINLINE static T * f( T & v, long ) - { - return reinterpret_cast( - &const_cast(reinterpret_cast(v))); - } - - BOOST_MOVE_FORCEINLINE static T * f( T * v, int ) - { return v; } -}; - -template -BOOST_MOVE_FORCEINLINE T * addressof( T & v ) -{ - return ::boost::move_detail::addressof_impl::f - ( ::boost::move_detail::addr_impl_ref( v ), 0 ); -} ////////////////////////////////////// // has_pointer_type diff --git a/test/adaptive_merge_test.cpp b/test/adaptive_merge_test.cpp index 1b6ebd3..01f7c3b 100644 --- a/test/adaptive_merge_test.cpp +++ b/test/adaptive_merge_test.cpp @@ -16,6 +16,7 @@ #include #include +#include #include "order_type.hpp" #include "random_shuffle.hpp" @@ -53,8 +54,8 @@ bool test_random_shuffled(std::size_t const element_count, std::size_t const num boost::movelib::unique_ptr buf(new char [sizeof(T)*(element_count-element_count/2)]); std::size_t const split = std::size_t(std::rand()) % element_count; - boost::movelib::merge_sort(elements.get(), elements.get()+split, order_type_less(), (T*)buf.get()); - boost::movelib::merge_sort(elements.get()+split, elements.get()+element_count, order_type_less(), (T*)buf.get()); + boost::movelib::merge_sort(elements.get(), elements.get()+split, order_type_less(), boost::move_detail::force_ptr(buf.get())); + boost::movelib::merge_sort(elements.get()+split, elements.get()+element_count, order_type_less(), boost::move_detail::force_ptr(buf.get())); boost::movelib::adaptive_merge(elements.get(), elements.get()+split, elements.get()+element_count, order_type_less()); diff --git a/test/bench_merge.cpp b/test/bench_merge.cpp index e5167bb..775a06e 100644 --- a/test/bench_merge.cpp +++ b/test/bench_merge.cpp @@ -22,6 +22,7 @@ #include #include +#include #include "order_type.hpp" #include "random_shuffle.hpp" @@ -70,14 +71,14 @@ template void adaptive_merge_buffered(T *elements, T *mid, T *last, Compare comp, std::size_t BufLen) { boost::movelib::unique_ptr mem(new char[sizeof(T)*BufLen]); - boost::movelib::adaptive_merge(elements, mid, last, comp, reinterpret_cast(mem.get()), BufLen); + boost::movelib::adaptive_merge(elements, mid, last, comp, boost::move_detail::force_ptr(mem.get()), BufLen); } template void std_like_adaptive_merge_buffered(T *elements, T *mid, T *last, Compare comp, std::size_t BufLen) { boost::movelib::unique_ptr mem(new char[sizeof(T)*BufLen]); - boost::movelib::merge_adaptive_ONlogN(elements, mid, last, comp, reinterpret_cast(mem.get()), BufLen); + boost::movelib::merge_adaptive_ONlogN(elements, mid, last, comp, boost::move_detail::force_ptr(mem.get()), BufLen); } enum AlgoType diff --git a/test/bench_sort.cpp b/test/bench_sort.cpp index d238043..079f2c8 100644 --- a/test/bench_sort.cpp +++ b/test/bench_sort.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include using boost::move_detail::cpu_timer; @@ -68,21 +69,21 @@ template void adaptive_sort_buffered(T *elements, std::size_t element_count, Compare comp, std::size_t BufLen) { boost::movelib::unique_ptr mem(new char[sizeof(T)*BufLen]); - boost::movelib::adaptive_sort(elements, elements + element_count, comp, reinterpret_cast(mem.get()), BufLen); + boost::movelib::adaptive_sort(elements, elements + element_count, comp, boost::move_detail::force_ptr(mem.get()), BufLen); } template void std_like_adaptive_stable_sort_buffered(T *elements, std::size_t element_count, Compare comp, std::size_t BufLen) { boost::movelib::unique_ptr mem(new char[sizeof(T)*BufLen]); - boost::movelib::stable_sort_adaptive_ONlogN2(elements, elements + element_count, comp, reinterpret_cast(mem.get()), BufLen); + boost::movelib::stable_sort_adaptive_ONlogN2(elements, elements + element_count, comp, boost::move_detail::force_ptr(mem.get()), BufLen); } template void merge_sort_buffered(T *elements, std::size_t element_count, Compare comp) { boost::movelib::unique_ptr mem(new char[sizeof(T)*((element_count+1)/2)]); - boost::movelib::merge_sort(elements, elements + element_count, comp, reinterpret_cast(mem.get())); + boost::movelib::merge_sort(elements, elements + element_count, comp, boost::move_detail::force_ptr(mem.get())); } enum AlgoType