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;
}
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;
}
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.
std::variant
, en.cppreference.com/w/cpp/utility/variant –
Shamble 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.
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/
© 2022 - 2024 — McMap. All rights reserved.
std
equivalent yet. – Poplitealboost::variant
is a header-only library, so you don't need to worry about building/linking the Boost library if you're only usingboost::variant
. Just make sure the path to boost header files is included in your project/IDE/makefile. – Nuptialsboost::optional
has been submitted once before and nothing came of it. – Rajasthanboost::variant
even be necessary? – Sohn