3 ms·
As a long-time user of C++ variants with a home-grown tool much like the article’s make_visitor, I think there’s a much bigger problem with C++ variants: the su
by amluto 6y ago
As a long-time user of C++ variants with a home-grown tool much like the article’s make_visitor, I think there’s a much bigger problem with C++ variants: the sub-types don’t have names.
If you genuinely want your variant to just be a mildly polymorphic object, this is okay. But you probably don’t. Imagine the classic use of variants for a parse tree:
variant<string, shared_ptr<Node>>
Okay, presumably the string is a token and the Node is a subtree. This is already a bit unpleasant, but at least it’s serviceable. But then you discover that you really want tokens and symbol names to be separate. You try:
variant<string, string, shared_ptr<Node>>
And you lose, because the two unnamed string cases are ambiguous. To work around this, you need to wrap the strings in newtypes, which is quite verbose in C++. And you start to wonder why, after all these years, C++ hasn’t sucked it up and come up with real sum types in the language.
(For those who are too stuck in C++ land, this problem simply does not exist in a language like Rust.)
- CodesInChaos 6y agoUnions discriminated by type could be useful in Rust, especially if you could use them without declaring the union type explicitly. It'd enable something like `Result<..., IoError | ParseError>` without having to manually create a union type, implement the appropriate traits for it, etc. Unfortunately it opens a can of worms related to type identity, so I don't expect to see such a feature soon.
- steveklabnik 6y agoThere are a group of people pushing for it ("anonymous enums") but it is unclear if it will ever happen or not, yes.
- quietbritishjim 6y ago> To work around this, you need to wrap the strings in newtypes That is certainly an option, but actually std::variant<string, string, shared_ptr<Node>> does still work for most operations (std::visit is not one of them though). So another option is to use that typelist directly and switch to index-based access, at least for those entries: if (std::string* s = std::get_if<0>(myVariant)) { // Use s } else if (std::string* s = std::get_if<1>(myVariant)) { // Use s } else if (NodePtr* node = std::get_if<2>(myVariant)) { // Use node } else { throw std::logic_error("Unknown variant entry"); } At that point you'll probably want to define an enum (an old-school one so it implicitly converts to int) to identify the cases symbollically, which admittedly is pretty messy given that it's up to you, the maintainer, to keep the enum in sync with the actual type list in the variant. But it's hardly the worst coding sin ever, and better than any alternative I can think of (short of switching to another language). As you can see from my comment further down, I think a chain of if statements is cleaner than std::visit anyway, precisely because of the complexity problems of std::visit discussed in the article.