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

std::variant 'attempting to reference a deleted function'

When trying to compile I get the error: Error C2280 'std::variant,Tree::Position,Tree::Position,Tree::Position>::variant(const…
KobeFl
  • 17
  • 8
1
vote
2 answers

Casting a variant to super set variant or a subset variant

I've adapted some code from this answer to handle the case in which the target variant is a subset of the source variant as follows: template struct variant_cast_proxy { std::variant v; template
jwezorek
  • 8,592
  • 1
  • 29
  • 46
1
vote
1 answer

How to call std::visit inside a lambda with a visitor that is a function object captured by value

It seems like it is not straightforward to call std::visit within a lambda using a visitor that is a function object captured by value. Capturing by reference works fine though. Why is this the case and is it somehow possible to do this? I do not…
noubius
  • 15
  • 4
1
vote
1 answer

Do elements in `std::array` as a class member get default intialised

Consider: class x { std::array data_; public: x() /*no reference to data_ here*/ {} }; Do the int elements in data_ get zeroed, or is their value indeterminate? By extension is that also true in this case: class x { …
111111
  • 15,686
  • 6
  • 47
  • 62
1
vote
1 answer

is it safe to pass std::variant over the network, cross platform

std::variant is a replacement of union. But union can pass over network and receive in another platform(different compiler or arch) safely. Can std::variant do so? For example, I have two machine A and B. A is windows, MSVC 19.4. B is Linux, gcc(or…
欢乐的Xiaox
  • 479
  • 4
  • 6
1
vote
1 answer

std::visit does not recognise types

I'm stumped that after some code refactoring, the following piece of code does not work anymore as in it jumps to the auto, auto case and ignores the Complex, Complex one. I admittedly don't quite understand what the overload is exactly doing, but…
infinitezero
  • 1,610
  • 3
  • 14
  • 29
1
vote
2 answers

std::visit and std::variant usage

#include #include #include #include template struct Promise { std::variant< std::monostate, std::conditional_t, std::monostate, T>, …
Carter Li
  • 149
  • 12
1
vote
2 answers

What will destructor of a std::variant do if it contains void* data

I have just started using std::variant in my projects. I have a doubt. What will the destructor of std::variant do in the code shown below. Variant holds a void* data. Once variant goes out of scope, I think it will only free the memory of void* but…
raju
  • 191
  • 1
  • 3
  • 12
1
vote
2 answers

Overloading a function with std::enable_if to avoid template substitution error

I want to write two template functions such that one catches a specific case and the other catches all other cases that don't match first case. I'm trying to use std::enable_if to catch the specific case, but the compiler still fails with an…
thayne
  • 426
  • 5
  • 18
1
vote
1 answer

How to use std::visit with std::variant containing enum

I try to use a std::variant with an enum as part of the possible types. I have a compile error and i don't find the reason. If I use any other type instead of the enum, the code works. Here a part of my code: #include #include…
Jonathan
  • 69
  • 1
  • 6
1
vote
1 answer

How to simplify std::variant class types

I am sure there is a an easy way to do this but couldn't find anything in SO. Couldn't find much info in en.cppreference.com either. Is there a way to simplify the std::variant so that we can declare functions and classes that could…
Yucel_K
  • 688
  • 9
  • 17
1
vote
1 answer

How is the memory layout of a std::vector of std::variant?

I have a std::vector of std::variant types allocated in stack. Since the size of each variant is variable. I am wondering what the memory layout of the vector is in stack.
motam79
  • 3,542
  • 5
  • 34
  • 60
1
vote
1 answer

Using std::variant> as a flexible input instead of subclassing

I have a class which takes an input, and sometimes I'd like to set that input by assigning a variable, and at other times I'd like the class to call a function to get its input. In the past, I'd have just used a std::function as the input, and…
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
0
votes
0 answers

Why does decltype fail to declare a reference type in lambda arguments?

I encountered an issue where I need to use std::visit() on two std::variant objects which are guaranteed to host the same type at the time of the call. The following code compiles without issues: std::variant var1 =…
Phiv
  • 1
  • 1
0
votes
2 answers

class with std::variant - custom [] operator based on variant content

I have a simple class: #include struct RawDataArray{ std::variant data; template constexpr bool IsType() const noexcept{ return std::holds_alternative(data); } template…
Martin Perry
  • 9,232
  • 8
  • 46
  • 114