How can I use std::variant
in g++
? Why isn't there std::variant
in std::experimental
(though std::optional
is)? What version of g++
do I need? I prefer not to use boost
and I'd like to use standard library only.
Edit: it seems like only g++ 7
supports std::variant
. Then my question is: when will it be released and what problems may I encounter when using its experimental version?
The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or in the case of error - no value (this state is hard to achieve, see valueless_by_exception).
std::variant (C++17)An instance of std::variant has a value from one of its types. The value must not be a reference, C-array or void. A std::variant can have one type more than once. A default-initialized std::variant will be initialized with its first type.
To print a variant, enter its name on the initial screen of the variant maintenance tool, select either Attributes or Values in the sub-objects group box and choose Print. Note that you cannot print the values if you are working in change mode. The Print Screen List screen appears.
std::visit from C++17 is a powerful utility that allows you to call a function over a currently active type in std::variant . In this post, I'll show you how to leverage all capabilities of this handy function: the basics, applying on multiple variants, and passing additional parameters to the matching function.
This page says, that GCC 7 has std::variant
.
GCC has an info page where you can check which language feature was implemented with which version. It looks like variant
is not yet implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With