Standards compliant variant
Recently, a proposal was made for a standards compliant variant, which can be found at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4450.pdf. Apparently much has been said about what such a beast would look like, since there are many options for how to handle the edge cases. Nevertheless, a proposal has been made, and here are its key features:
- It can be empty.
- No restrictions on the types that can be used with regards to no throw move assignable etc.
- Provides relational operators which can be used as long as each type
T op T, and uses the ordering in the type list if they are not the same type.
- The return type of visitation can be automatically deduced.
- All memory is inside the variant object, i.e., it uses no extra heap allocation.
Since my variant is almost there, I have started the work needed to make my variant fit the standard. It will end up being a superset of the standard since I support generic multi-visitation with arbitrary arguments (more on that in another post), and I have a mechanism for recursive variants.
To make my variant not empty there are only a few minor things to do:
- If the index is
-1on visitation, call the visitor’s
- Handle errors so that the index is set to
-1appropriately, and the existing object is destructed.
Otherwise my variant already works.
In starting this, I have noticed that
index is a
size_t, but that it can also be
-1 when empty. I’m not sure if this is an error, or if they intended it to mean the largest number that a
size_t can hold.