Why does std.variant not have a tag?

evansl cppljevans at suddenlink.net
Sun Nov 4 15:31:55 PST 2012


  http://dlang.org/phobos/std_variant.html

says:

 This module implements a discriminated union type (a.k.a. tagged union,
algebraic type).

Yet, the wiki page:

  http://en.wikipedia.org/wiki/Tagged_union

says:

  a tag field explicitly indicates which one is in use.

and I don't see any indication of a tag field in the std_variant.html
page.  Another wiki reference:

  http://en.wikipedia.org/wiki/Disjoint_union

is more explicit because it pairs the tag with the value:

  (x,i)

where x is the value and i is the tag.

One reason for an explicit tag is that the bounded types may contain
the same type twice.  This has lead to problems in boost::variant as
evidenced by the post:

  http://article.gmane.org/gmane.comp.parsers.spirit.general/17118

In addition, both variant and tuple have a common part, a metafunction
mapping from a tag to a type; hence, this same common part could be used
to implement both tuple and a tagged variant.

A variant which actually contained a tag field I think would be more
general in that it would allow duplicate types among the bounded types
just as a tuple's bounded types can contain duplicate types.

-regards,
Larry



More information about the Digitalmars-d mailing list