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
1
vote
0 answers

Getting the value of a constexpr std::variant via constexpr function

This is a reduced example of a bigger function. The core issue is that at one point I try to get the value out of a constexpr std::variant via a constexpr/consteval function. This fails and I do not know why. Code: #include #include…
1
vote
2 answers

Function accepting a reference to std::variant

I'm trying to pass values to a function accepting a std::variant. I noticed I can use a function accepting a const reference to a variant value, but not a reference alone. Consider this code #include #include #include…
Moia
  • 2,216
  • 1
  • 12
  • 34
1
vote
1 answer

Invoke a method for each alternative in a list of std::variant without macros

I have a set of Writer classes each with a different implementation. I have a list of writers that offers the same interface. Calling a method on the list should invoke the same method on each of the elements in the list (composite design pattern).…
doxygen
  • 14,341
  • 2
  • 43
  • 37
1
vote
1 answer

Generically wrap member function of object to modify return type

Version restriction: C++17. I'm trying to create a type capable of accepting a callable object of any type and wrapping one of its member functions (in this case, operator()) to take the same arguments, but modify (cast) the return type. An example…
user4520
  • 3,401
  • 1
  • 27
  • 50
1
vote
2 answers

How is it possible to unite multiple types in the operator function called by std::visit in C++?

I am using std::variant and std::visit to call operator functions. I have a lot of variants (which mostly inherit from one superclass), but most of the operator functions should return the same value. Is there a way to have one operator function,…
wittn
  • 298
  • 5
  • 16
1
vote
2 answers

Can't call function with variant parameter using initializer_list

I'm not so skilled in using std::variant and couldn't completely understand all constructor cases. using map_type = std::multimap; using union_type = std::variant; void foo(union_type) { ... } void…
mouse_00
  • 593
  • 3
  • 13
1
vote
3 answers

Accessing the struct members inside a std::variant

I've got a std::variant, which has as alternatives two different structs. Now I need to fill it with data for one alternative (which i know at runtime), but it has to be in a way, that it's possible to write the elements in the struct…
Gabriel
  • 11
  • 5
1
vote
1 answer

std::map with values of different function-types

I want to use a std::map with certain types of functions (certain, specific parameter types) as possible values. However, the following (minimal) example does not compile. Why is that and how can I allow FunctionType and FunctionType as…
Simon Fromme
  • 3,104
  • 18
  • 30
1
vote
0 answers

std::variant as last argument of template function taking multiple variadic arguments

I have the following code: #include #include #include template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...) ->…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

dealing with variants containing move-only types

Consider the following code: #include #include #include template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...)->overloaded; struct foo { int…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
1
vote
2 answers

vector of std::variant types to std::tuple

I am working on vector of std::variant types. Is there a way to convert it to std::tuple of the values holded by given std::variants ? typedef std::variant a_union; std::vector vec; For example, I would like to have tuple…
bring112
  • 63
  • 1
  • 5
1
vote
4 answers

Default-construct all the types in a std::variant and put them in a std::vector

How would I make a std::vector that contains a default-constructed instance of all the types contained in a std::variant? using TaskVariant = std::variant; std::vector variants; // I'd like to do this in a…
1
vote
1 answer

std::bad_variant_access error when trying to store value to std::variant in map

I'm parsing a file into a map with std::variant (to support two types of values) and I can't figure out the right way to store the value into the map. I'm getting an: std::bad_variant_access what(): unexpected index This is the map…
Niki
  • 33
  • 4
1
vote
1 answer

Why can't std::variant find operator<() when not in same namespace as compared classes

I was trying to provide a custom operator< for a class from an external library. This class is within that library's namespace, however, the operator I wanted to define is not. Now if I define a std::variant and want to use it in a std::set, the…
StefanKssmr
  • 1,196
  • 9
  • 16
1
vote
1 answer

std::variant visitor where the visitor modifies the variant

Is it safe to modify a variant inside the visitor function? struct visitor { visitor(std::variant & var) : var(var){} void operator()(int i) { var = std::to_string(i); } void operator()(const std::string &…
jo-art
  • 215
  • 1
  • 9