普通的默认可构造std :: optional和std :: variant [英] trivially default constructible std::optional and std::variant

查看:117
本文介绍了普通的默认可构造std :: optional和std :: variant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以这样设计std::optional(当前为std::experimental::optional),对于普通默认可构造类型T对应的std::optional< T >也是普通默认可构造类型?

Is it permitable to design std::optional (currently std::experimental::optional) in such a way, that for trivially default constructible type T corresponding std::optional< T > is also trivially default constructible?

相同的问题引起了std::variant及其不可分的鉴别器.

The same question regading std::variant and its integral discriminator.

我自己的回答是:不,不能以这种方式设计,因为如果对象具有自动存储期限,或者如果对象是非存储的reinterpret_cast -ed,则在默认初始化期间获取的整数标识符的值将不确定. -零初始化存储."在我看来,不允许用户每次都要进行值初始化.

My own answer is: "No, it cannot be designed in this way, because value of its integral discriminator obtained during default initialization will be indeterminate if the object has automatic storage duration or if it is reinterpret_cast-ed from non-zero-initialized storage." Requirement to the user to do value-initialization every time is not allowed on my mind.

推荐答案

您的答案是正确的:您不能.该规范要求在默认构造时将其初始化标志"设置为false.

Your answer is correct: you cannot. The specification requires that its "initialized flag" is set to false upon default construction.

这篇关于普通的默认可构造std :: optional和std :: variant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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