5 ms·
> it allows the variant to be default constructed even if none of its members are Ah, okay! So it's serving the same purpose as the "None" in this Rust snippet
by ryukoposting 2y ago
> it allows the variant to be default constructed even if none of its members are
Ah, okay! So it's serving the same purpose as the "None" in this Rust snippet:
enum Foo {
None, // <-- impl core::Default and return this
T(T),
U(U),
V(V),
}
That makes sense, though it really shows how the legacy of C++'s type system limits stdlib design in the present day. It'd be awfully nice to be able to just write std::variant<void, T, U, V>.
- OskarS 2y agoExactly. The difference here is that std::variant doesn’t “name” the options, you just get the type (and an index, so variant can contain duplicate types). So you have to have some marker type that’s like “this contains nothing”. Fundamentally, Rust’s enums are a much better way of doing this thing compared to std::variant, but C++ did the best it could without changing the core language. Which arguably they should have done.
- jcelerier 2y agoI mean, it's very trivial to make your own basic wrapper with "void" in the API if that's what you want: https://gcc.godbolt.org/z/v6EPTaaPY https://gcc.godbolt.org/z/v6EPTaaPY
- ryukoposting 2y agoI suppose that code is "trivial" in the same sense that anything else involving C++11 is "trivial."
- jcelerier 2y agothis took like less than 5 minutes to write, this is definitely in the "trivial" realm