Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::variant single storage guarantee

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?

like image 491
lurscher Avatar asked Oct 02 '11 18:10

lurscher


1 Answers

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.

like image 100
Omnifarious Avatar answered Sep 20 '22 04:09

Omnifarious