Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::variant in g++

Tags:

c++

g++

stl

variant

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?

like image 649
Aleksandr Tukallo Avatar asked Dec 22 '16 13:12

Aleksandr Tukallo


People also ask

What is std :: variant in C++?

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).

How does STD variant work?

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.

How do I print a variant?

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.

What does STD visit do?

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.


2 Answers

This page says, that GCC 7 has std::variant.

like image 75
Ralph Tandetzky Avatar answered Oct 08 '22 17:10

Ralph Tandetzky


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.

like image 31
user2393256 Avatar answered Oct 08 '22 16:10

user2393256