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
0
votes
1 answer

what is the cost of using std::visit

Consider the following code (run it) #include #include #include #include struct Student { std::string name; Student(const char* name) : name(name) {} void display() { fmt::print("Student {}\n",…
doraemon
  • 2,296
  • 1
  • 17
  • 36
0
votes
1 answer

std::variant Serialization and Deserialization in Runtime

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…
YlmzCmlttn
  • 3
  • 1
  • 3
0
votes
0 answers

std::variant in "variables or watch" window in VS Code while debugging

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…
C. Binair
  • 419
  • 4
  • 14
0
votes
3 answers

How to create an array of callables based on a std::variant

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 {…
fluter
  • 13,238
  • 8
  • 62
  • 100
0
votes
1 answer

Call generic method for std::variant

There are two classes with methods f() under the same name and signature: struct A { void f() {} }; struct B { void f() {} }; Is it possible having a std::variant v to call this method with a single expression instead of…
0
votes
0 answers

What is the most runtime-performant and safest enum-like type I can define in C++?

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 ==…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
2 answers

emplace pointer as proper type into std::variant in template

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…
Brosbiln
  • 23
  • 4
0
votes
0 answers

Passing std::variant to a function C++20

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…
M. McIlree
  • 109
  • 3
0
votes
1 answer

Translating Swift enums with associated values to C++

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…
Rob N
  • 15,024
  • 17
  • 92
  • 165
0
votes
2 answers

std::variant having for possible type a ptr to an array of a given type. Can I store more information that just type?

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…
user18490
  • 3,546
  • 4
  • 33
  • 52
0
votes
0 answers

Managing protocol headers and payload with std::variant?

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…
DavSanchez
  • 831
  • 8
  • 13
0
votes
0 answers

Range-based loop with object using variant of vectors

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…
0
votes
1 answer

How to define class with overloaded methods for each std::variant alternative?

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…
jinscoe123
  • 1,467
  • 14
  • 24
0
votes
1 answer

invalid declarator before std::variant

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…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

how do I replace base constructor call

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: …
TruthSeeker
  • 1,539
  • 11
  • 24