6 ms·
It will be great to see variants as a standard part of the language. Variants provide a way of doing a sort of type safe union for different types of data.
by setra 10y ago
It will be great to see variants as a standard part of the language. Variants provide a way of doing a sort of type safe union for different types of data.
- quotemstr 10y agoThe new variant feature is based on boost::variant, which you can use today. Getting the thing into the standard library is nice and all, but it doesn't open up new capabilities the way that new syntax does.
- hellofunk 10y agoThe implementation of variant in C++17 is very different than in Boost. The Boost variant has a double buffer for the entire variant, and one of these buffers is always kept on the heap even if your variant is on the stack! This acts as a safety net incase the changing of the variant's value were to throw, you can get the old value back. This implementation did not fly for C++'s goal of being high performance, and thus the new standardized variant is done quite differently.
- n00b101 10y agoI did not have a good experience with boost::variant ... Compile times increased drastically, compiler error messages became enormous and garbled with templates, syntax was onerous in places (e.g. "make_recursive_variant"), and identification of types is based on arbitrary index value that is risky to rely on (which() method). I ended up refactoring the whole thing, removing boost::variant and just using the traditional visitor pattern. The only advantage boost::variant really has over traditional C++ visitor pattern is that boost static_visitor functions can have a non-void return type ... Being able to directly return a value in the visitor functions is admittedly very convenient, but the other issues negate this convenience.
- meetingcpp 10y agoboost::variant is 12 years old, the std::variant version is not based on the actual design nor is it a copy of the boost implementation. The interface is very similar, but the implementation is ofc using C++17 and not C++03.
- rleigh 10y agoThe boost implementation is certainly showing its age at this point, as is mpl, now we have variadic templates. Are there any C++14 variant implementations compatible with boost::variant and std::variant which are based on variadic templates? I'd love to move to a more modern implementation if possible.
- mattnewport 10y agoMy understanding is it is partly an enabling step to later introduce some sort of pattern matching along the lines of the work Bjarne had been doing so new syntax will likely come.
- humanrebar 10y ago> but it doesn't open up new capabilities the way that new syntax does Taking very normal so-what code and moving it into the standard is pretty much the point. Having variant in std:: lets new code use variants without adding a boost dependency. I say that without knocking on boost. Fewer dependencies are always better.