Boost C++ Libraries: Ticket #11309: is_virtual_base_of has false positives caused by sizeof alignment https://svn.boost.org/trac10/ticket/11309 <p> The is_virtual_base_of type&lt;Base, Derived&gt; type trait can report virtual bases in situations when there isn't any. Here's an example code snippet where it fails: </p> <pre class="wiki">#include &lt;iomanip&gt; #include &lt;iostream&gt; #include &lt;boost/type_traits.hpp&gt; struct A { int a; }; struct B : public virtual A {}; struct C : public A { virtual ~C() {} }; int main() { std::cout &lt;&lt; std::boolalpha; std::cout &lt;&lt; "Is A a virtual base of B? " &lt;&lt; boost::is_virtual_base_of&lt;A, B&gt;::value &lt;&lt; std::endl; std::cout &lt;&lt; "Is A a virtual base of C? " &lt;&lt; boost::is_virtual_base_of&lt;A, C&gt;::value &lt;&lt; std::endl; return 0; } </pre><p> In the above code both checks are true despite A being a non-virtual base of C. The reason for this is that alignment is affecting the results from sizeof and it’s making two different sizes seem equal. </p> <p> In particular: </p> <pre class="wiki">sizeof(is_virtual_base_of_impl&lt;A, C, mpl::true_&gt;::boost_type_traits_internal_struct_X) == 16 sizeof(is_virtual_base_of_impl&lt;A, C, mpl::true_&gt;::boost_type_traits_internal_struct_Y) == 16 </pre><p> However, if we redefine C as packed (example for gcc / clang): </p> <pre class="wiki">struct C : public A { virtual ~C() {} } __attribute__((packed); </pre><p> Then the sizes do mismatch as expected and the result is correct: </p> <pre class="wiki">sizeof(is_virtual_base_of_impl&lt;A, C, mpl::true_&gt;::boost_type_traits_internal_struct_X) == 16 sizeof(is_virtual_base_of_impl&lt;A, C, mpl::true_&gt;::boost_type_traits_internal_struct_Y) == 12 </pre><p> The solution for this problem would be to define the auxiliary types <em>boost_type_traits_internal_struct_X</em> and <em>boost_type_traits_internal_struct_Y</em> as packed in such a way that works across all supported compilers. This might be some combination of <em>_ _attribute_ _((packed))</em> for gcc/clang, <em>#pragma pack</em> for the Visual C++ compiler and others. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11309 Trac 1.4.3 John Maddock Sat, 16 May 2015 17:41:22 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11309#comment:1 https://svn.boost.org/trac10/ticket/11309#comment:1 <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> Attributes didn't help, but this seems to get it: <a class="ext-link" href="https://github.com/boostorg/type_traits/commit/24b7d226d3316d36711c40b7ca854c2b2d4565be"><span class="icon">​</span>https://github.com/boostorg/type_traits/commit/24b7d226d3316d36711c40b7ca854c2b2d4565be</a> </p> Ticket