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
3
votes
0 answers

No template named 'variant' in namespace 'std'

In XCode 10.3 I am getting the error message when I try to build -> No template named 'variant' in namespace 'std' for the lines of code -> #include typedef std::variant
Adam Freeman
  • 1,271
  • 12
  • 20
3
votes
2 answers

Why does a std::variant compile with begin and end iterators?

It seems that the compiler should be able to catch the fact that std::variant doesn't have iterator methods, but it seems that my code compiles with no problem (even if I randomly make up methods or member variables for the variant), but it crashes…
mriera
  • 261
  • 1
  • 11
3
votes
1 answer

How to get a reference to variant’s value?

I have std::variant where all classes are derived from the same base. I want to cast variant to base. return std::visit( []( const Base& b ) { return b; }, v ); This compiles but gives warning C4172: returning address of local variable or…
Soonts
  • 20,079
  • 9
  • 57
  • 130
3
votes
3 answers

how to return a specific type from a variant using a visitor?

I have the code below and why visitor1 and visitor2 gives errors? Does that mean the visitor cannot return one type within the variant? #include #include struct Visitor1 { template T operator()(const T & t)…
shelper
  • 10,053
  • 8
  • 41
  • 67
3
votes
1 answer

Why `std:variant`'s `operator=(T&& t)`'s noexcept spec doesn't depend on inner types's destructor's noexcept spec?

Long title: Why std:variant's operator=(T&& t)'s noexcept specification doesn't depend on inner types's destructor's noexcept specification? I can see on cppreference that template variant& operator=(T&& t) noexcept(/* see below…
Mate059
  • 103
  • 5
2
votes
1 answer

Is it okay to inherit from an std::variant in order to make it recursive?

This question comes out of a reddit thread about an Advent of Code problem from last year. I had mentioned that the only way to make an std::variant recursive was to wrap it in a struct so you can forward declare the struct as there is no way to…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
2
votes
2 answers

std::variant behavior leads to an error when calling std::get inside std::visit: Is it intended?

I'm currently writing a method in C++17 where a std::variant is visited through std::visit. The variant is embedded in a object that is passed by reference to the lambda function used in std::visit, and this object is then used in the function to…
RaymoAisla
  • 161
  • 1
  • 7
2
votes
1 answer

Recursive typedef definition using std::variant

I want to define a std::variant that can store a vector of pairs, with strings and Values. I want to create a structure like below: typedef std::variant>> Value; How can I do it in C++17?
Stuart
  • 25
  • 4
2
votes
0 answers

struct without constructors containing std::variant containing array cannot be returned by value?

In C++ I want to return a struct by value which contains a variant with an array inside. The struct has no explicit constructors. Is this not possible? In the following I get the compiler error (gcc): "error: use of deleted function…
Ernie Mur
  • 481
  • 3
  • 18
2
votes
2 answers

How to efficiently initialize a std::variant data member in a class template

Consider the following class template, that can hold either a value of type T or an instance of some ErrorInfo class, using a std::variant data member: template class ValueOrError { private: std::variant
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
2
votes
2 answers

Unable to visit a variant if one of its alternatives does not have a specific field

I am trying to visit a variant containing several classes. One of them does not have a specific field value but I handle it with constexpr, however, the compiler still fails to compile. #include #include struct A {}; struct B…
fsquirrel
  • 800
  • 8
  • 18
2
votes
1 answer

Is it safe to call std::visit on classes with virtual functions?

I'd like to create a hierarchy of visitors to handle instances of std::variant. I'd like to override some of the methods in subclasses for more specialized behavior. My code looks like this: #include #include #include…
Presto
  • 219
  • 1
  • 7
2
votes
1 answer

C++ variant containing a map of itself

I would like to be able to create a variant that contains a std::map as one of its cases. The ideal would be able to write something like using MyVariant = std::variant
user17789309
2
votes
4 answers

"increment" `std::variant` alternative

I want to increment / decrement a std::variant's type alternative, essentially like so: using var_t = std::variant; var_t var; var.emplace< (var.index()+1) % std::variant_size >(); // "increment" case, wrapping for good measure The…
nonchip
  • 1,084
  • 1
  • 18
  • 36
2
votes
1 answer

Extract a c++ variant type from a list of type names

I am designing a pipeline class that needs to extract a std::variant from a list of distinct types in a filter class. For example: template struct Filter { using type = T; // ... } template struct Pipeline { …
Jes
  • 2,614
  • 4
  • 25
  • 45