Tagged unions (aka variant) in C++ with the same type multiple times

boost, c++, ocaml, variant

Solution

If you want to do this, I think your best option is to wrap the same-but-different-types into a struct which then lets the boost variant visit the proper one:

struct Speed
{
    float val_;
};

struct Darkness
{
    float val_;
};

You might be able to use `BOOST_STRONG_TYPEDEF` to do this automatically but I'm not sure it's guaranteed to generate types legal for use in a union (although it would probably be fine in a variant).

Problem

I need to create an union, but 2 members of the union would have the same type, thus I need a way to identify them. For example in OCaml : ``` type A = | B of int | C of float | D of float ``` Boost.Variant doesn't seem to support this case, is there a known library which supports that ?

Original source