c++ - boost::variant 单一存储保证

标签 c++ boost-variant

我的目标是保证所有变体类型的单一存储:根据 'never empty' guarantee from Boost::variant ,我们需要覆盖 boost::has_nothrow_copy 每个有界类型。但过了一会儿 该文档提到了一些关于 'boost::blank' 的内容,如果 类型已绑定(bind),变体将设置该值而不是尝试不抛出 默认复制构造函数。

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

最佳答案

我相信这已经很明确了。以下是 boost 文档中的相关部分:

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.

因为 boost::blank 是不可抛出默认构造的,所以第二个子句适用。听起来 Boost 对这种特殊类型进行了特殊处理,以支持所有其他类型,因此与其未指定将实例化哪个默认可构造类型,不如保证该类型是 boost::blank 如果这是一个选项。

关于c++ - boost::variant 单一存储保证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7628498/

相关文章:

c++ - 使用任意键停止程序(c++)

c++ - 如何正确使用QProcess写?

c++ - 原子会遭受虚假存储吗?

c++ - 不存在从 "std::string"到 "char"的合适转换

c++ - 使用重新解释转换将结构或类保存到文件

c++ - boost::spirit::karma 使用替代运算符 (|) 和条件

c++ - boost::variant 隐式转换为字符串

c++ - boost::变体用法

c++ - "Cannot convert parameter"使用 boost::variant 迭代器

c++ - boost 变体支持多少种数据类型?