boostorg / container

STL-like containers from Boost
http://www.boost.org/libs/container/
Boost Software License 1.0
96 stars 116 forks source link

operator -> on static_vector::iterator causes cast alignment warning #206

Closed jtbr closed 2 years ago

jtbr commented 2 years ago

Using operator-> on static_vector::iterator, gcc (6.3.1 arm-none-eabi) gives the following warning with -Wcast-align:

boost_1_64_0\boost\move\detail\meta_utils.hpp: In instantiation of 'static T* boost::move_detail::addressof_impl<T>::f(T&, long int) [with T = a_struct]':
boost_1_64_0\boost\move\detail\meta_utils.hpp:270:7:   required from 'T* boost::move_detail::addressof(T&) [with T = a_struct]'
boost_1_64_0\boost\intrusive\pointer_traits.hpp:292:48:   required from 'static T* boost::intrusive::pointer_traits<T*>::pointer_to(boost::intrusive::pointer_traits<T*>::reference) [with T = a_struct; boost::intrusive::pointer_traits<T*>::pointer = a_struct*; boost::intrusive::pointer_traits<T*>::reference = a_struct&]'
boost_1_64_0\boost\container\vector.hpp:126:69:   required from 'boost::container::container_detail::vec_iterator<Pointer, IsConst>::pointer boost::container::container_detail::vec_iterator<Pointer, IsConst>::operator->() const [with Pointer = a_struct*; bool IsConst = false; boost::container::container_detail::vec_iterator<Pointer, IsConst>::pointer = a_struct*]'
mycode.cpp:12:20:   required from here
boost_1_64_0\boost\move\detail\meta_utils.hpp:258:14: warning: cast from 'char*' to 'a_struct*' increases required alignment of target type [-Wcast-align]
       return reinterpret_cast<T*>(
              ^~~~~~~~~~~~~~~~~~~~~
          &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

De-referencing the iterator before accessing the member gives no warning. Here is a minimal example with the warning:

#include "boost/container/static_vector.hpp"

struct a_struct {
    int a = 1;
};
void myExample() {
    typedef boost::container::static_vector<a_struct, 1> vec_type;
    int something=10;
    vec_type vec;
    vec.push_back({2});
    vec_type::iterator it = vec.begin();
    something += it->a; // <--- gives the warning
    something += (*it).a; // <--- gives no warning
}

I'm using boost 1.64, but nothing about this seems to have changed in the latest code. The same warnings also appear with GCC 11.2.0 (mingw) -Wcast-align=strict.

igaztanaga commented 2 years ago

Thanks for the report. Supporting Wcast-align support requires several changes in dependent libraries. I've prepared the following commits to prepare the patch for Boost.Container: