Skip to content

Standards compliant variant

December 16, 2015

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 provides 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 -1 on visitation, call the visitor’s operator()()
  • Handle errors so that the index is set to -1 appropriately, 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.

Advertisements

From → Uncategorized

4 Comments
  1. Andreas Weis permalink

    Note that the N4550 proposal for std::variant was highly controversial and has undergone quite some changes since April.

    I gave a lightning talk at Munich’s C++ user group a couple of weeks ago, where I tried to summarize the latest developments in this regard.

    If you are interested, you can find my slides at

    • Thanks I’ll check it out. Yes I realise that there was a lot of controversy since there are so many choices to make. Do you know if there is much more written about the changes since April?

      • Never mind that last comment. I looked at your slides and I can see that there are various proposals in open-std.org.

  2. Jeff Schwab permalink

    Thanks for the summary. Re. index -1, there’s a parenthetical clarification:

    • changes index() to return -1 (now also known is tuple_not_found) if empty()

    The later definition of tuple_not_found in n4450 seems inspired by string::npos:

    static constexpr const size_t tuple_not_found = (size_t) -1;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: