c++ - std::is_trivially_xxx 哪一个暗示另一个
问题描述
我很难理解以下概念:
- is_trivially_copyable
- is_trivially_copy_assignable
- is_trivially_copy_constructible
- is_trivially_destructible
- is_trivially_move_assignable
举个例子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 不感兴趣:
- is_trivially_constructible
- is_trivially_default_constructible
解决方案
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.
推荐阅读
- amazon-s3 - AWS S3 对象生命周期排除
- css - 防止 Bootstrap 4 自动填充列换行到下一行
- ruby - 在 Ruby 中将哈希作为参数传递
- c# - SKCanvas.DrawCircle() 正在绘制正方形
- excel - 将 Excel 的文本框添加到 Powerpoint VBA
- c# - 使用 sigcheck 工具将 streamreader 数据输出到 csv
- amazon-web-services - Google Cloud 是否为其计算实例提供公共主机名?
- php - 在 Woocommerce 上更改代码功能内的文本颜色
- arrays - 多个数组到数据框熊猫
- excel - 使用形状的位置删除行