std::is_trivially_xxx which one imply another

Viewed 108

I have hard time to understand following concepts:

  • is_trivially_copyable
  • is_trivially_copy_assignable
  • is_trivially_copy_constructible
  • is_trivially_destructible
  • is_trivially_move_assignable

Take for example std::string_view.

#include <string_view>

int main(){
    using sv = std::string_view;

//    static_assert(std::is_trivial_v<sv> );
    static_assert(std::is_trivially_copyable_v<sv> );
    static_assert(std::is_trivially_copy_assignable_v<sv> );
    static_assert(std::is_trivially_copy_constructible_v<sv> );
    static_assert(std::is_trivially_destructible_v<sv> );
    static_assert(std::is_trivially_move_assignable_v<sv> );
}

My question is which of these, imply others, so I can to less static_assert's in my code?

I know is_trivial imply all of these,

Note I am not interested in c-tors:

  • is_trivially_constructible
  • is_trivially_default_constructible
1 Answers

std::is_trivially_copyable covers the rest, though allows for the relevant methods to be deleted. It says:

If T is a TriviallyCopyable type, provides the member constant value equal true. For any other type, value is false.

And requirements for TriviallyCopyable are:

  • Every copy constructor is trivial or deleted
  • Every move constructor is trivial or deleted
  • Every copy assignment operator is trivial or deleted
  • Every move assignment operator is trivial or deleted
  • at least one copy constructor, move constructor, copy assignment operator, or move assignment operator is non-deleted
  • Trivial non-deleted destructor

You can even check llvm implementation.

This is related to the rule of zero.

AFAIK rest of the test are basic and unrelated to each other in terms of logical implications.

Apart from tests you mention is_copy_assignable and is_copy_constructible (and possibly other "copy" versions) can be formulated in terms of no-copy tests e.g. is_assignable. But this is something different, since checking for "copy" is just adding additional type constraint.

Related