首页 > 解决方案 > std::is_trivially_xxx 哪一个暗示另一个

问题描述

我很难理解以下概念:

举个例子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> );
}

我的问题是这些中的哪一个暗示了其他人,所以我可以在我的代码中减少 static_assert 的内容?

我知道is_trivial暗示所有这些,

注意我对 c-tors 不感兴趣:

标签: c++c++17

解决方案


std::is_trivially_copyable涵盖其余部分,但允许删除相关方法。它说:

如果 T 是TriviallyCopyable类型,则提供等于 true 的成员常量值。对于任何其他类型,值为 false。

TriviallyCopyable的要求是:

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


推荐阅读