Boost C++ Libraries: Ticket #13479: GCC-warning: break strict-aliasing rules in <boost/type_traits/integral_constant.hpp> https://svn.boost.org/trac10/ticket/13479 <p> Currently, I receive the GCC-warning 'warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]' about the (indirect included) header-file &lt;boost/boost/type_traits/integral_constant.hpp&gt; (e.g. line 93), if: </p> <ul><li>I compile with GCC 4.6.4 or 6.4.0, and </li><li>boost 1.60.0 or 1.65.1, and </li><li>the option -Wstrict-aliasing=2 is used; not with on level 1 or 3 </li><li>and optimization-level -O2 or -Os (or above), not with -O0,-O1 (or -Og on GCC 6) </li></ul><p> IMHO, the reinterpret_cast from &amp;data (I.e.g casting from const char*) causes this issue (and not using the dereference-helper afterwards, which comment shows that this is a GCC-work-around). </p> <p> As work-around I'm using an additional const void* - helper-variable (ptr) instead of using &amp;data directly, which seems to satisfy the compiler: </p> <pre class="wiki"> operator const mpl::bool_&lt;val&gt;&amp; ()const { static const char data = 0; void const* ptr = &amp;data; return dereference(reinterpret_cast&lt;const mpl::bool_&lt;val&gt;*&gt;(ptr)); } </pre><p> ... similar code-snippet also for 'operator const mpl::integral_c&lt;T, val&gt;&amp; ()const' on line 65 ... [Note: using 'void const* const ptr =&amp;data;' does not fix it] </p> <p> As alternative, an additional cast '(uintptr_t)&amp;data' within reinterpret_cast seems to fix/inhibit this compiler-warning also. </p> <p> At least, is my fix/work-around a feasible solution (without any side-effects)? </p> <p> Best regards from Salzburg, Markus </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13479 Trac 1.4.3 ibiatiroler@… Wed, 14 Mar 2018 12:31:43 GMT <link>https://svn.boost.org/trac10/ticket/13479#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13479#comment:1</guid> <description> <p> Additional information - I.e., tested with "official" MinGW-GCC 6.3.0 </p> <p> Source-file "test.cpp": </p> <pre class="wiki">#include &lt;boost/type_traits/integral_constant.hpp&gt; extern bool test(boost::mpl::bool_&lt;true&gt; const&amp;); bool result = test(boost::true_type()); </pre><p> Compile will result in following warning: </p> <pre class="wiki">&gt; 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&lt;bool, val&gt;::operator const mpl_::bool_&lt;val&gt;&amp;() 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&lt;const mpl::bool_&lt;val&gt;*&gt;(&amp;data)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </pre><p> 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 ... </p> <pre class="wiki">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&lt;T, val&gt; type; static const T value = val; - // - // This helper function is just to disable type-punning - // warnings from GCC: - // - template &lt;class U&gt; - static U&amp; dereference(U* p) { return *p; } operator const mpl::integral_c&lt;T, val&gt;&amp; ()const { - static const char data[sizeof(long)] = { 0 }; - return dereference(reinterpret_cast&lt;const mpl::integral_c&lt;T, val&gt;*&gt;(&amp;data)); + static const long data = 0; + const void* ptr = &amp;data; + return *(reinterpret_cast&lt;const mpl::integral_c&lt;T, val&gt;*&gt;(ptr)); } BOOST_CONSTEXPR operator T()const { return val; } }; @@ -80,17 +75,12 @@ typedef bool value_type; typedef integral_constant&lt;bool, val&gt; type; static const bool value = val; - // - // This helper function is just to disable type-punning - // warnings from GCC: - // - template &lt;class T&gt; - static T&amp; dereference(T* p) { return *p; } operator const mpl::bool_&lt;val&gt;&amp; ()const { static const char data = 0; - return dereference(reinterpret_cast&lt;const mpl::bool_&lt;val&gt;*&gt;(&amp;data)); + const void* ptr = &amp;data; + return *(reinterpret_cast&lt;const mpl::bool_&lt;val&gt;*&gt;(ptr)); } BOOST_CONSTEXPR operator bool()const { return val; } }; </pre><p> Is my suggested patch a suitable fix and does replace the the dereference-helper (which does not work for me on -Wstrict-aliasing=2)? </p> <p> Best regards from Salzburg, Markus </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Thu, 29 Mar 2018 10:49:34 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/13479#comment:2 https://svn.boost.org/trac10/ticket/13479#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Apologies for not getting to this sooner, fixed in <a class="ext-link" href="https://github.com/boostorg/type_traits/commit/4c3706113a5a130dcc66a13da6134e5ede122e33"><span class="icon">​</span>https://github.com/boostorg/type_traits/commit/4c3706113a5a130dcc66a13da6134e5ede122e33</a> Not sure if it will make the next release or not as it's already in beta. </p> Ticket