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
Hello I want to create Serialization class to serialize my variables settings to file or buffer. I was doing good until I start to implement std::variant in my Class. As far as I understand. I can not get variant types in runtime. It need to…
I'm trying to debug my code in Visual Studio Code, but I run into an issues when trying to inspect an std::variant. The "VARIABLES or WATCH" window just shows me: variable_name: {...}. There is a dropdown arrow, but if I press it nothing shows up.
I…
How can I create an array of functions that based off a std::variant which composed of a few message types, where they are to be decoded from io bytes, so that I can quickly access the right functions based on a mid field from the bytes.
struct S1 {…
Assume all I need is a type Color inhabited by only 3 values, red, green, blue, (just like bool is inhabited just by 2 values, true and false), and the ability to tell those 3 values apart from each other, i.e. the ability to test that e.g. red ==…
Is it possible to emplace some void* pointer into variant with a runtime generated index.
Can this nice solution (from here link), be updated to get index and pointer to emplace?
#include
template
void…
I'm working on someone else's (C++20) codebase using Clion and the IDE is giving me this error (although the code itself is compiling and runnable using g++12):
In template: attempt to use a deleted function error occurred here in instantiation of…
In Swift you can have an enum type with associated values.
enum Thing {
case num(Int)
case two(String, Double)
case other
}
var t: Thing = .num(123)
t = .two("a", 6.022)
t = .other
From what I'm reading, you can can do a similar thing in…
I am looking at using std::variant to store basic types such as float, int, float2, float2, bool2, bool4, etc. which is rather trivial, but I would also ideally like to construct variant objects holding a pointer to a typed array. I came up with…
Suppose I am trying to represent the contents of a tar file in a C++ struct. Each block of a tar file can be a header (which in turn has 2 possible versions) or a payload, all in blocks of 512 bytes (padded for the headers). Each possible form of a…
I am attempting to figure out how to achieve runtime polymorphism with a class called Signal, where its data type is passed as enum value to the constructor. Compile-time polymorphism with templates is not suitable.
As all values in the "signal" are…
I have some std::variant classes, each with several alternatives, and I would like to define a visitor class template that takes a variant as its template parameter and will automatically define a pure virtual void operator()(T const&) const for…
I'm trying to implement an ad-hoc light weight state machine using std::variant. However, it seems that the variant fsm isn't declared right as it fails with the following errors:
: In function 'int main()':
:235:40: error: invalid…
In recent SO answer, part of the snippet I'm unable to understand whats happening,
struct VariableDepthList : std::variant, int> {
private:
using base = std::variant, int>;
public:
…