c++language-lawyertrivially-copyable

Are deleted constructors still considered as trivial?


Considering the following class:

#include <type_traits>

struct S
{
    S(const S &) = delete;
    S(S &&) = delete;
    S & operator=(const S &) = delete;
    S & operator=(S &&) = delete;
};

From the C++ standard (11.2 §1):

A trivially copyable class is a class:

  • that has at least one eligible copy constructor, move constructor, copy assignment operator, or move assignment operator ([special], [class.copy.ctor], [class.copy.assign]),
  • where each eligible copy constructor, move constructor, copy assignment operator, and move assignment operator is trivial, and
  • that has a trivial, non-deleted destructor ([class.dtor]).

emphasis mine

Then, per (11.4.5.3 §11):

A copy/move constructor for class X is trivial if it is not user-provided and if

  • class X has no virtual functions ([class.virtual]) and no virtual base classes ([class.mi]), and
  • the constructor selected to copy/move each direct base class subobject is trivial, and
  • for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial; otherwise the copy/move constructor is non-trivial.

emphasis mine


Unless I missed something, the above class should not be considered trivially copyable according to (my understanding of) the standard.

Surprisingly, the following compile-time assertion passes with both gcc and clang:

static_assert(std::is_trivially_copyable_v<S>);

Live example

If that is a bug, it seems very unlikely to me that both compilers would share the same bug.

Consequently, my guess is that deleted constructors may not be considered as user-provided which would make them eligible for the trivially-copyable trait.

But... I cannot find anywhere in the standard whether or not this claim is verified.


Solution

  • Deleted functions are neither user-provided ([dcl.fct.def.default]/5) nor eligible ([special]/6.1), so they can be trivial but cannot contribute to trivial copyability.