boost::variant 单一存储保证

发布于 2024-12-07 19:39:59 字数 410 浏览 1 评论 0原文

我的目标是保证所有变体类型的单一存储:根据 来自 Boost::variant 的“永不为空”保证,我们需要重写 对于每个有界类型,boost::has_nothro_copy。但过了一会儿 该文档提到了一些关于 'boost::blank' 的内容,如果是的话 类型已绑定,变体将设置该值而不是尝试不抛出 默认复制构造函数。

不清楚的是是否在有界类型列表中添加 boost::blank 将避免覆盖/专门化 has_nothro_copy 的要求 与其他类型?

My goal is to guarantee single storage on all my variant types: according to 'never empty' guarantee from Boost::variant, we need to override
boost::has_nothrow_copy for each bounded type. But a bit later
the documentation mentions something about 'boost::blank' and if that
type is bound, variant will set that value rather than try to nothrow
default copy constructors.

what is not clear is if adding boost::blank in the bounded type list
will avoid the requirement of overriding/specializing has_nothrow_copy
with the other types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-12-14 19:39:59

我相信这一点已经说得很清楚了。以下是 boost 文档中的相关部分:

因此,变体旨在实现以下优化
一旦满足其有界类型的以下条件:

对于每个不可抛出复制构造的有界类型 T(如
由 boost::has_nothro_copy 指示),库保证变体
T 将仅使用单一存储和就地构建。

如果有的话
有界类型不可抛出默认构造(如
boost::has_nothro_constructor),该库保证变体将
对每个有界的仅使用单一存储和就地构造
输入变体。但请注意,如果发生分配
失败,未指定的无抛出默认可构造有界类型
将在左侧操作数中默认构造,以便
保留永不为空的保证。

由于 boost::blank 不可构造默认抛出异常,因此适用第二个子句。听起来 Boost 已经对这个特定类型进行了特殊处理,以有利于所有其他类型,这样就可以保证类型为 boost::blank 如果这是一个选项。

I believe that is made clear. Here is the relevant section from the boost documentation:

Accordingly, variant is designed to enable the following optimizations
once the following criteria on its bounded types are met:

For each bounded type T that is nothrow copy-constructible (as
indicated by boost::has_nothrow_copy), the library guarantees variant
will use only single storage and in-place construction for T.

If any
bounded type is nothrow default-constructible (as indicated by
boost::has_nothrow_constructor), the library guarantees variant will
use only single storage and in-place construction for every bounded
type in the variant. Note, however, that in the event of assignment
failure, an unspecified nothrow default-constructible bounded type
will be default-constructed in the left-hand side operand so as to
preserve the never-empty guarantee.

Since boost::blank is nothrow default constructible, the second clause applies. And it sounds like Boost has special-cased this particular type to be chosen in favor of all others, so that instead of it being unspecified which default constructible type will be instantiated the type is guaranteed to be boost::blank if that's an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文