Re: [Boost-bugs] [Boost C++ Libraries] #13479: GCC-warning: break strict-aliasing rules in <boost/type_traits/integral_constant.hpp>

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #13479: GCC-warning: break strict-aliasing rules in <boost/type_traits/integral_constant.hpp>
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2018-03-14 12:31:43


#13479: GCC-warning: break strict-aliasing rules in
<boost/type_traits/integral_constant.hpp>
-------------------------------+--------------------------
  Reporter: ibiatiroler@… | Owner: John Maddock
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: type_traits
   Version: Boost 1.65.0 | Severity: Problem
Resolution: | Keywords: GCC
-------------------------------+--------------------------

Comment (by ibiatiroler@…):

 Additional information - I.e., tested with "official" MinGW-GCC 6.3.0

 Source-file "test.cpp":
 {{{
 #include <boost/type_traits/integral_constant.hpp>

 extern bool test(boost::mpl::bool_<true> const&);
 bool result = test(boost::true_type());
 }}}
 Compile will result in following warning:
 {{{
> gcc -I /Temp/SDK/boost -O2 -Wstrict-aliasing=2 -c test.cpp
 In file included from test.cpp:2:0:
 /Temp/SDK/boost/boost/type_traits/integral_constant.hpp: In instantiation
 of 'boost::integral_constant<bool, val>::operator const
 mpl_::bool_<val>&() const [with bool val = true]':
 test.cpp:6:38: required from here
 /Temp/SDK/boost/boost/type_traits/integral_constant.hpp:93:29: warning:
 type-punning to incomplete type might break strict-aliasing rules
 [-Wstrict-aliasing]
           return dereference(reinterpret_cast<const
 mpl::bool_<val>*>(&data));
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 }}}

 This will not occur, if I do apply following patch (as unified diff);
 I.e., using an helper-variable (ptr) and removing dereference-helper,
 which is (IMHO) no longer needed ...
 {{{
 Index: boost/type_traits/integral_constant.hpp
 ===================================================================
 --- boost/type_traits/integral_constant.hpp
 +++ boost/type_traits/integral_constant.hpp
 @@ -55,17 +55,12 @@
        typedef T value_type;
        typedef integral_constant<T, val> type;
        static const T value = val;
 - //
 - // This helper function is just to disable type-punning
 - // warnings from GCC:
 - //
 - template <class U>
 - static U& dereference(U* p) { return *p; }

        operator const mpl::integral_c<T, val>& ()const
        {
 - static const char data[sizeof(long)] = { 0 };
 - return dereference(reinterpret_cast<const mpl::integral_c<T,
 val>*>(&data));
 + static const long data = 0;
 + const void* ptr = &data;
 + return *(reinterpret_cast<const mpl::integral_c<T, val>*>(ptr));
        }
        BOOST_CONSTEXPR operator T()const { return val; }
     };
 @@ -80,17 +75,12 @@
        typedef bool value_type;
        typedef integral_constant<bool, val> type;
        static const bool value = val;
 - //
 - // This helper function is just to disable type-punning
 - // warnings from GCC:
 - //
 - template <class T>
 - static T& dereference(T* p) { return *p; }

        operator const mpl::bool_<val>& ()const
        {
           static const char data = 0;
 - return dereference(reinterpret_cast<const
 mpl::bool_<val>*>(&data));
 + const void* ptr = &data;
 + return *(reinterpret_cast<const mpl::bool_<val>*>(ptr));
        }
        BOOST_CONSTEXPR operator bool()const { return val; }
     };
 }}}

 Is my suggested patch a suitable fix and does replace the the dereference-
 helper (which does not work for me on -Wstrict-aliasing=2)?

 Best regards from Salzburg,
 Markus

-- 
Ticket URL: <https://svn.boost.org/trac10/ticket/13479#comment:1>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2018-03-14 12:37:21 UTC