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;
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
cached
  • 569
  • 1
  • 7
  • 14
  • 1
    There is not, but Boost.Variant is extremely likely to be in TR2. – ildjarn Mar 22 '12 at 22:01
  • 1
    Sadly, most of boost has no `std` equivalent yet. – Mooing Duck Mar 22 '12 at 22:05
  • 9
    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. – Kerrek SB Mar 22 '12 at 22:12
  • 4
    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. – Emile Cormier Mar 22 '12 at 22:49
  • @ildjarn: Was Boost.Variant proposed to the committee? I don't recall seeing the paper in the most recent mailing. – Nicol Bolas Mar 22 '12 at 23:53
  • @Nicol : Not yet, but [Beman said not to worry about it](http://lists.boost.org/Archives/boost/2012/02/190619.php). :-P – ildjarn Mar 22 '12 at 23:58
  • @ildjarn That sounds overly optimistic. If I recall correctly, at least `boost::optional` has been submitted once before and nothing came of it. – pmr Mar 29 '12 at 11:54
  • A bit of a necro-post, but now that C++ has unrestricted unions, would `boost::variant` even be necessary? – Evan Teran Aug 29 '14 at 13:53
  • 1
    @EvanTeran - Unrestricted unions may make it easier to implement a variant type, but they are not a substitute for them. – Ferruccio Dec 19 '14 at 17:17

3 Answers3

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • How does this qualify as an answer? This is a comment. – Brett Hale Jul 15 '16 at 07:32
  • This answer will soon be outdated indeed. C++17 will include `std::variant`, http://en.cppreference.com/w/cpp/utility/variant – TimZaman Aug 07 '16 at 11:55
  • @TimZaman: Thanks for the reminder. I added an edit to note C++17 support. – John Zwinck Aug 07 '16 at 17:10
  • @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?](http://stackoverflow.com/questions/40201371/what-are-the-differences-between-stdvariant-and-boostvariant?noredirect=1&lq=1]) – einpoklum Oct 23 '16 at 10:10
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.

Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
  • Can you list some of these changes? – einpoklum Apr 08 '16 at 17:24
  • 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. – Ralph Tandetzky Apr 11 '16 at 13:22
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/

Curious
  • 20,870
  • 8
  • 61
  • 146