我的目标是保证所有变体类型的单一存储:根据 '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/