Skip to main content
Filter by
Sorted by
Tagged with
Advice
0 votes
9 replies
184 views

Is it possible to map the enum values to underlaying data types to use them with std::variant? I want to do something like the example below, but want the std::variant to understand it is an int or ...
sav's user avatar
  • 76
0 votes
2 answers
157 views

I'm trying to use code as below, but I am getting compilation errors. The goal here is to have the "unsupported type in visitor triggered at compile time when a D is passed to visitor. Can ...
user3613174's user avatar
0 votes
0 answers
67 views

I have different enums to specify the type of an object. I then use std::variant as an entity can be of any type, i.e. enum Type1{ type1a, type1b }; enum Type2{ type2a, type2b }; ...
random_acct's user avatar
0 votes
0 answers
36 views

I'm curious if there's a way to avoid repeating myself. I have several classes that are not related by parentage: #include <pybind11/operators.h> #include <pybind11/pybind11.h> #include &...
Leonardo's user avatar
  • 2,020
2 votes
2 answers
141 views

I have the following code that does not compile with Qt 6.7.2 on Debian : #include <QCoreApplication> #include <iostream> #include <variant> class Foo : public QObject { ...
vazlsky's user avatar
  • 2,360
2 votes
2 answers
184 views

How to implement a class that dynamically switching types using std::variant in runtime? I'm currently trying to create a class that can: Store a value in a std::variant with configurable type ...
NeKon's user avatar
  • 314
1 vote
2 answers
141 views

I am at a very early stage in writing a C++ program replicating the most basic functions of the ls bash command. I use the <filesystem> header. The std::filesystem::directory_iterator and std::...
Giogre's user avatar
  • 1,664
1 vote
1 answer
109 views

It's possible to store ints or floats in the same memory location using std::bit_cast (including ints larger than 2^24 i.e. they can't be stored as whole numbers in floats because the mantissa has too ...
greenlagoon's user avatar
1 vote
2 answers
140 views

I'm struggling with an odd behavior of the below code. The problem is that output of this snippet is "double". But we have an int in lhs. I dug for a while and figured out the compiler put a ...
msg's user avatar
  • 166
1 vote
1 answer
76 views

for this simple code #include <vector> #include <variant> #include <stdint.h> using KFPGAData = std::variant<uint16_t>; int main(int,char**) { int v = 10; KFPGAData d =...
libgcc's user avatar
  • 193
1 vote
2 answers
117 views

I want to store several different callable objects (different types) in a container (e.g. std::array) in order to invoke them later. Each callable has the same return type and the same amount and type ...
breiters's user avatar
  • 158
2 votes
2 answers
278 views

I have a std::variant of possibly repeated types, and I would like to increment the active index of the variant. Here is an example demonstrating what I would like to do: template <typename... Ts&...
Bernard's user avatar
  • 5,800
8 votes
3 answers
4k views

Let's say I am writing a class that passes a std::variant<A,B> to another class. While I design the class, I decide I'd like to keep a copy of the last thing that was passed to that other class. ...
Eternal's user avatar
  • 3,114
0 votes
1 answer
110 views

I see this piece of code for implementing a C++ std::variant-like type. This class is used as the variant storage. What does the buffer array stores? alignas(Types...) unsigned char buffer[sizeof(...
user11611653's user avatar
3 votes
1 answer
196 views

I'm currently downporting C++17 code to C++14. Since C++14 doesn't have variant I'm using the variant implementation by mapbox which is available here. Basically, substituting variant using mapbox::...
Andreas's user avatar
  • 10.5k
0 votes
1 answer
125 views

How to access std::variant using std::get by specifying the type info during runtime. Below is a non working code that I tried #include <iostream> #include <string> #include <variant>...
Harry's user avatar
  • 4,146
4 votes
1 answer
311 views

If you have a std::variant with duplicate types, how do you assign to it? std::variant seemingly allows duplicate types. std::variant<int,int> foo; compiles successfully, and yet I am unsure ...
Algorithmic Canary's user avatar
2 votes
0 answers
41 views

I have been trying to make a game in Visual Studio using c++17, and I created a class type called ErrorType to represent all the error numeric values (nan, inf, etc). By using std::variant<long ...
RandomGuy's user avatar
  • 105
2 votes
1 answer
105 views

This question is based on previous SO discussion (which was affected by non-compliant compilers). So I'm using the latest c++23 released versions of gcc/clang/MSVC. Here is a simple test with ...
Gene's user avatar
  • 754
0 votes
0 answers
67 views

I have a message processing interface that accepts messages of different types. What I have now goes along the lines of this: Demo #include <iostream> enum class MsgType { write, read, ...
glades's user avatar
  • 5,374
2 votes
1 answer
105 views

Code: #include <iostream> #include <string> #include <vector> #include <variant> struct ignore { template <typename T> void operator()([[maybe_unused]]const T&...
anti-walker's user avatar
0 votes
1 answer
141 views

I have a class BASE which contains a member variable of type std::variant which can contain an object of type A or B. Based on the input to the constructor at runtime, I want to initialize this ...
pisoir's user avatar
  • 226
0 votes
0 answers
81 views

Using std::visit in parallel with std::variant, does it give a better performance in comparison with dynamic inheritance using virtual? Based on cppreference there is the following statement On ...
getsoubl's user avatar
  • 1,117
1 vote
3 answers
127 views

I have this code #include <unistd.h> #include <vector> #include <iostream> using namespace std; std::string join(std::vector<std::variant<std::string, std::string_view>>...
Shivanshu Arora's user avatar
5 votes
1 answer
296 views

What am I trying to achieve I want to write a lib that calls a method, with known form, on user defined types. I want that method to accept as parameters, other user defined types. Both categories of ...
Christopher Pisz's user avatar
1 vote
1 answer
529 views

If I create a std::variant using classes for the types, and add an explicit copy constructor to one of those classes, the compiler complains. With the default or a non-explicit copy constructor it ...
Ronnie Shipman's user avatar
2 votes
1 answer
102 views

I wrote the following program that works with msvc c++17 but rejected by gcc and clang. I want to know which compiler is right here. Demo. #include <variant> struct C { std::variant<bool&...
user avatar
-1 votes
2 answers
200 views

I have the following vectors std::vector<std::pair<std::string, std::function<std::string(const PlayerFRDb&)>>> stringExtractors = { {" TEAM ", extractTeam}, {&...
konradtrubilo's user avatar
1 vote
3 answers
153 views

I'm working on a problem (it's essentially an encoding problem), where the encoded type can be one of the 3 types: The first type is fully described by 2 int8_ts. I can use std::pair to contain this ...
24n8's user avatar
  • 2,328
3 votes
1 answer
89 views

So I have two variants (I have shortened them, they have more types): std::variant<int *, double *> PtrVariant; std::variant<int, double> ValueVariant; I want to copy value between them. ...
PiotrK's user avatar
  • 113
3 votes
1 answer
416 views

Wanted to use a std::map<std::string, std::variant<A, B>> my_map where A and B are classes that have similar constructors (take a yaml object as argument), but I cannot figure out how to ...
ManikSin's user avatar
3 votes
2 answers
202 views

I was writing a compile-time parser but I am stuck on a problem which I don't know how to solve in C++. I am using Microsoft Visual Studio Community 2019, version 17.8.3 (latest). This is the code ...
Aakash Gupta's user avatar
0 votes
1 answer
126 views

The use-case I'm trying to build a small interpreter for a text-based command console. E.g., say there's a function somewhere: void SetBrightness(float brightness) { /* ... */ } Then it should be ...
alexpanter's user avatar
  • 1,646
5 votes
1 answer
473 views

I have two structs A1 and A2 and another struct B has constructors for both. How do I write a delegating constructor for std::variant<A1,A2>? #include <variant> struct A1 {}; struct A2 {}; ...
phinz's user avatar
  • 1,647
3 votes
1 answer
401 views

The following code does not compile. Why is this restriction, and how can I bypass it? If I replace the variant declaration, it compiles. struct PPP { int xxx; PPP() : x(xxx) {} // error: No ...
DuduArbel's user avatar
  • 1,298
1 vote
1 answer
188 views

In the code below, is there a way to implement const_foobar foobar_manager::get_foobar() const in terms of the non-const version without the visit or equivalent code? #include <variant> #include ...
jwezorek's user avatar
  • 10k
3 votes
0 answers
167 views

With regards to the advice in this answer: How to guarantee copy elision with std::variant discovered after this question was closed. TLDR: MSVC doesn't work with the advice given. https://godbolt.org/...
bradgonesurfing's user avatar
3 votes
1 answer
350 views

See https://godbolt.org/z/Pv8xfeE17 fails #include <iostream> #include <variant> #include <vector> struct T { int _x; T(int x) : _x(x) { std::cout << "constructed&...
bradgonesurfing's user avatar
3 votes
1 answer
236 views

I'm trying my luck with the spaceship operator. Say I have a variant-like object (that I conveninently derive from std::variant with fixed template parameters) and I want to define operators ==, <, ...
glades's user avatar
  • 5,374
6 votes
2 answers
984 views

For example, I'm trying to implement an AST using std::variant, where a Token can be a number, keyword, or variable. Where numbers are represented by the int and keywords and variables are represented ...
Jemtaly's user avatar
  • 140
1 vote
2 answers
789 views

I am working on a lexer. I have a Token struct, which looks like this: struct Token { enum class Type { ... }; Type type; std::string_view lexeme; } The Token's lexeme is just a view ...
kto's user avatar
  • 13
11 votes
2 answers
7k views

I already know how to use std::variant fairly well with std::get_if(), std::get() and std::visit(). But, what if I just want a simple way to tell if a variant has ever been initialized to any value? ...
Joe's user avatar
  • 7,194
6 votes
1 answer
453 views

I have a class C that contains a struct S and a std::variant as members. The struct S has an int member a that is initialized to 0. Here is the code: #include <variant> class C { struct S { ...
Sir2B's user avatar
  • 1,079
2 votes
1 answer
699 views

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 ...
jwezorek's user avatar
  • 10k
-2 votes
2 answers
79 views

I am working on a piece that requires some compile-time data structures to be created from a run-time provided mapping. If the runtime provided mapping matches the pre-defined compile-time pattern, ...
user3641187's user avatar
0 votes
0 answers
89 views

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::...
Phiv's user avatar
  • 1
0 votes
2 answers
142 views

I have a simple class: #include <variant> struct RawDataArray{ std::variant<double*, float*> data; template <typename T> constexpr bool IsType() const noexcept{ ...
Martin Perry's user avatar
  • 9,585
2 votes
2 answers
797 views

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, ...
RaymoAisla's user avatar
1 vote
1 answer
460 views

Consider the following code (run it) #include <fmt/core.h> #include <string> #include <array> #include <variant> struct Student { std::string name; Student(const char* ...
doraemon's user avatar
  • 2,604
3 votes
2 answers
813 views

Consider the following function for determining if variables of identical variant types are holding the same type: #include <variant> #include <iostream> template <typename... Ts> ...
jwezorek's user avatar
  • 10k