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

395 views Asked by At

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 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.

1

There are 1 answers

3
eerorika On
  • At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is eligible
  • Has a trivial non-deleted destructor

First and last requirement - check. But the other requirements are super-strong

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

You could use (is_copy_constructible || is_move_constructible || is_assignable) && std::is_trivially_destructible.

I'd be willing to "compromise" on having a trivial ... copy ctor as a requirement

You could use std::is_trivially_copy_constructible.

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.

However, those requirements won't be sufficient for this. You need the "super-strong" requirements for this. This is what std::is_trivially_copyable is for.