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

问题描述

在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]:

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

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 已经具有。当其析构函数很琐碎时,它就是。只能在常量表达式中创建和处理文字类型的对象。

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.

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

10-28 23:32