7 ms·
Is this the first type of sum-type option choosing statement present for C++ unions? I've been waiting for this feature since the year 1978. Still, it's a wast
by mFixman 7mo ago
Is this the first type of sum-type option choosing statement present for C++ unions? I've been waiting for this feature since the year 1978.
Still, it's a wasted opportunity not to have a language-level overload to the `switch` statement that allows nice pattern matching. Even with std::is_within_lifetime C++ unions are prone to errors and hard to work with.
- sebtron 7mo agoSince C++17 there is std::variant https://en.cppreference.com/w/cpp/utility/variant.html https://en.cppreference.com/w/cpp/utility/variant.html
- maleldil 7mo agoThese are not proper sum types, since you're limited to one variant per type.
- sebtron 7mo agoI am not sure what you mean, you can definitely have e.g. an std::variant<int, std::string, bool> Which is a sum of those three types.
- maleldil 7mo agoSee the first IpAddr example here[1], where you have separate variants, both with string representations. You can't do this with std::variant. You have to use separate types. [1] https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html
- sebtron 7mo agoI see what you mean now, thanks. To reproduce that example with std::variant I would need some kind of strong type alias, which as far as I know is missing from C++; so the only feasible way to do that would be wrapping the string in another class or struct.