Questions tagged [std-variant]

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

139 questions
92
votes
4 answers

`std::variant` vs. inheritance vs. other ways (performance)

I'm wondering about std::variant performance. When should I not use it? It seems like virtual functions are still much better than using std::visit which surprised me! In "A Tour of C++" Bjarne Stroustrup says this about pattern checking after…
The Moisrex
  • 1,857
  • 1
  • 14
  • 16
25
votes
2 answers

Why is my struct destructed twice with `std::variant` and `std::monostate`?

I am trying to learn std::variant. I do not understand why in this example, where I prefer not to initialize ab yet, and I use std::monostate for that, the class A gets constructed once, but destructed twice. What is happening? #include…
Chiel
  • 6,006
  • 2
  • 32
  • 57
25
votes
4 answers

How can I code something like a switch for std::variant?

I have some var = std::variant when a, b, c is some types. How, at runtime, do I check what type var contains? In the official documentation I found information that if var contains a type and I write std::get(var) I get…
dasfex
  • 1,148
  • 2
  • 12
  • 30
24
votes
1 answer

May I change the held type in a std::variant from within a call to std::visit

Does the following code invoke undefined behaviour? std::variant v = ...; std::visit([&v](auto& e){ if constexpr (std::is_same_v,A>) e.some_modifying_operation_on_A(); else { int i =…
burnpanck
  • 1,955
  • 1
  • 12
  • 36
20
votes
3 answers

Split a given std::variant type by a given criteria

How to by a given variant type using V = std::variant>; declare two variant types using V1 = std::variant; using V2 = std::variant
Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57
15
votes
1 answer

std::variant behaves differently in MSVC and gcc

Update: This is a C++ standard defect, which is fixed in C++20 (P0608R3). Also, VS 2019 16.10 has fixed this bug with /std:c++20. MSVC 19.28 rejects the following code but gcc 10.2 accepts it and outputs true false #include #include…
El Mismo Sol
  • 869
  • 1
  • 6
  • 14
14
votes
3 answers

How is std::optional never "valueless by exception"?

std::variant can enter a state called "valueless by exception". As I understand, the common cause of this is if a move assignment throws an exception. The variant's old value isn't guaranteed to be present anymore, and neither is the intended new…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
14
votes
3 answers

In C++, how to make a variant that can contain a vector of of same variant?

I a trying to make a std::variant that can contain a vector of the same variant: class ScriptParameter; using ScriptParameter = std::variant >; I am getting ScriptParameter redefinition.…
JeffV
  • 52,985
  • 32
  • 103
  • 124
12
votes
3 answers

Why isn't std::variant allowed to equal compare with one of its alternative types?

For example, it should be very helpful to equal compare a std::variant with a T1 or T2. So far we can only compare with the same variant type.
wanghan02
  • 1,227
  • 7
  • 14
12
votes
1 answer

Why is sizeof( std::variant< char > ) == 8 when using libc++ and not 2 (like with MSVC's STL and libstdc++)?

Consider this example on Compiler explorer. Basically, we have this code snippet: #include #include enum class Enum1 : std::uint8_t { A, B }; enum class Enum2 : std::uint8_t { C, D }; using Var = std::variant< Enum1, Enum2…
DoDo
  • 2,248
  • 2
  • 22
  • 34
11
votes
3 answers

Is there any practical reason why std::get_if (std::variant) takes a variant argument by pointer instead of by value/&/const&?

I've never used std::get_if, and since its name is different from std::get, I don't see a reason why its argument should be a pointer¹ (whereas std::get has a by-reference parameter). ¹If it was named std::get too, then overload resolution would be…
Enlico
  • 23,259
  • 6
  • 48
  • 102
11
votes
1 answer

What should I use instead of void as one of the alternative types in an variant?

I want to have a variant which may contain type Foo, (disjoint) type Bar, or nothing. Well, naturally, I was thinking of using std::variant - but this doesn't seem to work. That is, you can define this type, but if you try to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
10
votes
4 answers

How to improve compiler error messages when using C++ std::visit?

I am using C++17's std::visit() function on a variant with many alternatives, and the error messages produced by the compiler whenever I forget one or more of the alternatives in my visitor are quite difficult to understand. e.g. template
jinscoe123
  • 1,467
  • 14
  • 24
10
votes
1 answer

Why do std::variant implementations take more than 1 byte for variant of empty types?

This is mostly trivia question, as I doubt that I will ever need this space saving. While playing around on godbolt I noticed that both libstdc++ and libc++ implementations of std::variant require more than 1 byte to store variant of empty…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
10
votes
4 answers

std::bind to a std::variant containing multiple std::function types

I'm playing around with callback functions and wish to register multiple functions via std::bind that differ in signatures (altough they all return void). Assigning the result of the std::bind to the std::variant yields in a "conversion to…
Tmplt
  • 127
  • 1
  • 7
1
2 3
9 10