jeudi 21 avril 2022

std::is_trivially_copyable is too strong, what shall I use instead?

I want to accept classes which are "trivially copyable", in the sense that if I mem-copy the byte representation of one variable of the type into another, it will be usable, and nothing will have been broken in any way.

Looking at std::is_trivially_copyable, I see the requirements are:

Trivially copyable classes, i.e. classes satisfying following requirements:

  • At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is eligible
  • Every eligible copy constructor (if any) is trivial
  • Every eligible move constructor (if any) is trivial
  • Every eligible copy assignment operator (if any) is trivial
  • Every eligible move assignment operator (if any) is trivial
  • Has a trivial non-deleted destructor

First and last requirement - check. But the other requirements are super-strong and not what I want! I'd be willing to "compromise" on having a trivial (=defaulted) copy ctor as a requirement, and that's already quite different than what I really want.

So, what type trait can I use from the standard library, or write myself, to express the constraint I'm interested in?

I'm writing C++11 right now, so if your answer requires a later standard - write it, but mention that fact explicitly.

Aucun commentaire:

Enregistrer un commentaire