重大破坏的意义 [英] Significance of trivial destruction

查看:81
本文介绍了重大破坏的意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 17中,新的 std :: optional 要求,如果 T 为在[optional.object.dtor]中几乎可破坏的:

In C++17, the new std::optional mandates that it be trivially destructible if T is trivially destructible in [optional.object.dtor]:


〜optional();

1 效果:如果 is_trivially_destructible_v< T> != true * this 包含一个值,调用 val-> T ::〜T()

2 备注:如果 is_trivially_destructible_v< T> == true ,则此析构函数将是琐碎的析构函数。

~optional();
1 Effects: If is_trivially_destructible_v<T> != true and *this contains a value, calls val->T::~T().
2 Remarks: If is_trivially_destructible_v<T> == true then this destructor shall be a trivial destructor.

因此,此潜在实现片段将是非-符合标准:

So this potential implementation fragment would be non-conforming to the standard:

template <class T>
struct wrong_optional {
    union { T value; };
    bool on;

    ~wrong_optional() { if (on) { value.~T(); } }
};

我的问题是:此任务授权的好处是什么?大概,对于微不足道的可破坏类型,编译器可以弄清楚 value。〜T()是空操作,并且不会为<$​​ c $ c> wrong_optional< T> :: ~~ rong_optional()。

My question is: what is the advantage of this mandate? Presumably, for trivially destructible types, the compiler can figure out that value.~T() is a no-op and emit no code for wrong_optional<T>::~wrong_optional().

推荐答案

std :: optional 已经具有 constexpr 构造函数。当其析构函数很琐碎时,它就是文字类型。只能在常量表达式中创建和处理文字类型的对象。

std::optional already has constexpr constructors. When its destructor is trivial, it is a literal type. Only objects of literal types can be created and manipulated in constant expressions.

这篇关于重大破坏的意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆