Hacker News new | past | comments | ask | show | jobs | submit login
Rant: On the C++ std::experimental::variant to come (fusionfenix.com)
34 points by ingve on Oct 19, 2015 | hide | past | favorite | 11 comments



This is one area where I think Rust has a clear advantage of no baggage. They get to represent Some(T) as a null pointer in the None case.

I will say that having being exposed to Rust's Enums(sum types) I don't think I can ever go back. Combined with pattern matching it's a hell of a combo.


As someone with twenty years of C++ experience, I will say I don't see why that would be difficult to implement in C++... it doesn't even seem hard... it is the kind of thing I would have been writing over ten years ago, actually: you just have a partial template specialization of a private base class that takes a boolean predicate as a template parameter to check for "one of these types has a sentinel value and the other type can be represented as a sentinal value" (you could even keep a count of sentinal as you lay out multiple types: count up the number that can be represented as sentinels and then verify all types have at least that many available sentinels; some types could seriously be friendly and provide a near infinite number of sentinels do to having otherwise invalid states, and this would be quite trivially handled in a generic way any type could slot into), and then you can use the null-or-not to collapse the storage of Some(T) and None.


This isn't true in general, it's only true when T is a pointer type, in which case C++ would just use a pointer.


Or if T is NonZero.

More importantly, dereferencing a None is not undefined behavior in Rust.


Or if T contains a NonZero (Vec, VecDeque, HashMap) ;)


> Or if T is NonZero.

Ok, I wasn't aware of this.

>More importantly, dereferencing a None is not undefined behavior in Rust.

That's an unrelated point. I never said Rust enum's have no advantages.


It seems like that wasn't the case for the proposed C++ variant. It seems like you would have to use some sort of sentinel value to discriminate between None and Some(or virtual which is even worse).

Rust explicitly omits the sentinel value and leaves it to the compiler so that it can decide how to optimize best.


I'm just saying you wouldn't use std::variant<T star>, you'd just use T star. Also C++ has the meta-programming capabilities to special variant for pointers.

edit: replaced * with star to defeat italicizer.


And then you have to deal with null pointers.


This is an unrelated point, we were talking about representation.


boost::variant is really interesting and useful in my opinion but it seems to increase compile times exponentially (to say nothing of compiler error messages) ... If a language based variant in C++ could solve those issues then I think that would be really great.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: