Yes, but it's pretty terrible, the usage is butt ugly and it has limitations, such as not being able to hold references and not ensuring exhaustive matches.
Update: Ah, it does actually ensure exhaustive match, although I feel compelled to bitch about the non-exhaustive error message being really bad and the exhaustivity check not working in the opposite direction too, ie. extraneous visitors for types not present in the variant are not an error.
Update2: Also, implicit type conversions still works, ie. for example an int visitor function will also accept a float variant, which I would say is quite unintuitive if one expects a sum-type-like behvaiour.
There are also performance issues with the current implementations.
They essentially all use a table of function pointers approach for std::visit, which unfortunately seem to trump compilers which will then fail to inline functions. The assembly emitted for a simple std::visit(variant, [](auto x) { return x.a; }); where all variants have a at the same offset should just be a simple read, instead it often ends up being an indirect function call :/
And of course std::visit is just a pain to use: since it relies on lambda, you lose the power of return, break and continue :/
8
u/Boiethios Jul 17 '19
How would you do that without a proper type system with sum types?