What is the equivalent of boost::variant in the C++ standard library?
Asked Answered
W

3

13

I am looking for an alternative to C-style union. boost::variant is one such option. Is there anything in std C++ ?

union {
   int i;
   double d;
}
Widely answered 22/3, 2012 at 22:0 Comment(9)
There is not, but Boost.Variant is extremely likely to be in TR2.Odalisque
Sadly, most of boost has no std equivalent yet.Popliteal
Nothing wrong with using Boost. The whole point of C++ is that it lets you write libraries that do useful things. It doesn't try to provide everything out of the box, but rather, it gives you the tools to make everything you need. Use Boost.Supereminent
Please note that boost::variant is a header-only library, so you don't need to worry about building/linking the Boost library if you're only using boost::variant. Just make sure the path to boost header files is included in your project/IDE/makefile.Nuptials
@ildjarn: Was Boost.Variant proposed to the committee? I don't recall seeing the paper in the most recent mailing.Daven
@Nicol : Not yet, but Beman said not to worry about it. :-POdalisque
@Odalisque That sounds overly optimistic. If I recall correctly, at least boost::optional has been submitted once before and nothing came of it.Rajasthan
A bit of a necro-post, but now that C++ has unrestricted unions, would boost::variant even be necessary?Sohn
@EvanTeran - Unrestricted unions may make it easier to implement a variant type, but they are not a substitute for them.Scansion
D
25

As several commenters said: No, there is no Boost Variant-alike in standard C++. Maybe in a few years there will be, but why wait--use Boost Variant today!


Edit (four years later, 2016): In C++17 there will be std::variant. Similar but not identical to boost::variant. So when your compiler supports C++17, you will have a solution in the standard library.

Disclosure answered 22/3, 2012 at 23:12 Comment(4)
How does this qualify as an answer? This is a comment.Denton
This answer will soon be outdated indeed. C++17 will include std::variant, en.cppreference.com/w/cpp/utility/variantShamble
@TimZaman: Thanks for the reminder. I added an edit to note C++17 support.Disclosure
@JohnZwinck: I would be glad if you could have a look at a question I just asked following this answer of yours: What are the differences between std::variant and boost::variant?Sommer
D
10

A few years passed: Now a proposal is on the way. Possibly boost::variant will make it into C++17! Until then we will have to live with boost::variant which is still awesome. Note that there will be some changes in the details in std::variant.

UPDATE: The proposal made it. std::variant will be part of the new C++17 standard. Here's the June 2016 meeting report by Herb Sutter. There he confirms it.

Darksome answered 11/12, 2015 at 13:23 Comment(2)
Can you list some of these changes?Sommer
The changes are still subject to change. ;) Some changes are controversial. Also it is rather not on the surface but there are some complicated design decisions. In order to not complicate the answer unnecessarily, I chose to be concise and to the point. Some of the issues can be found in the link to the proposal in my answer.Darksome
C
4

std::variant is now officially going to be a part of the C++17 standard library! https://herbsutter.com/2016/06/30/trip-report-summer-iso-c-standards-meeting-oulu/

Consubstantiation answered 7/7, 2016 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.