Boost C++ Libraries: Ticket #11844: memcpy should only be used on trivially copyable objects https://svn.boost.org/trac10/ticket/11844 <p> This is a follow up to this ticket: </p> <p> <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/11229"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/11229</a> </p> <p> Boost.Container currently checks the <code>is_trivially_copy_constructible</code> and <code>is_trivially_copy_assignable</code> type traits to enable <code>memcpy</code> optimization. However, the standard says that <code>memcpy</code> may only be used with <em>trivially copyable</em> types, and being <em>trivially copy constructible/assignable</em> is not a sufficient, or indeed necessary, condition for trivial copyability. Trivially copyable types must: </p> <ul><li>Have no non-trivial copy constructors </li><li>Have no non-trivial copy assignment operators </li><li>Have no non-trivial move constructors </li><li>Have no non-trivial move assignment operators </li><li>Have a trivial destructor </li></ul><p> Note that deleted special functions are classified as trivial. This is required, otherwise types such as this would not be trivially copyable: </p> <pre class="wiki">struct X { const int x; }; </pre><p> This is why I say that trivially copy constructibility is not a necessary condition for trivial copyability. </p> <p> Note that there is a proposal to modify the definition of trivially copyable so that at least one special function has to not be deleted (and the destructor must not be deleted): </p> <p> <a class="ext-link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4460.html"><span class="icon">​</span>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4460.html</a> </p> <p> Using <code>memcpy</code> with types that aren't trivially copyable is <em>undefined behaviour</em> according to the standard. Thus, the behaviour of Boost.Container is technically undefined when used with types that are trivially copy constructible/assignable but <em>not</em> trivially copyable. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11844 Trac 1.4.3 Ion Gaztañaga Wed, 03 Aug 2016 21:16:22 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11844#comment:1 https://svn.boost.org/trac10/ticket/11844#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> Thanks for the report, as mentioned in the related ticket, the library now checks is a type is copy constructible or copy assignable to implement its internal "is_trivially_copy_assignable" and "is_trivially_copy_constructible" types. Commited in: </p> <p> <a class="ext-link" href="https://github.com/boostorg/move/commit/e7d24400cb986efaefa0acbff5de6e74eae876d9"><span class="icon">​</span>https://github.com/boostorg/move/commit/e7d24400cb986efaefa0acbff5de6e74eae876d9</a> </p> Ticket