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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With