183 questions
Advice
0
votes
9
replies
184
views
map enum values to data types
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 ...
0
votes
2
answers
157
views
cannot use static_assert for unsupported type
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 ...
0
votes
0
answers
67
views
Is it possible to add a std::variant as a component to an entity in EnTT?
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
};
...
0
votes
0
answers
36
views
Exposing `std::variant` as `TypeAlias` with Pybind11
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 &...
2
votes
2
answers
141
views
std::visit for QObject's
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
{
...
2
votes
2
answers
184
views
How to implement a runtime type switching class in C++
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 ...
1
vote
2
answers
141
views
Use of `std::variant` to select types after command prompt input, is that even possible?
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::...
1
vote
1
answer
109
views
Allowing different fundamental types to share the same memory location by using std::bit_cast instead of std::variant or union?
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 ...
1
vote
2
answers
140
views
Weird result of std::variant
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 ...
1
vote
1
answer
76
views
gcc 10 refuse implict conversions for std::variant constructor
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 =...
1
vote
2
answers
117
views
Container of std::variant of callable objects with bound parameters w/o using dynamic memory
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 ...
2
votes
2
answers
278
views
Get a compile-time index to a std::variant when visiting it? [duplicate]
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&...
8
votes
3
answers
4k
views
When and why should I use std::monostate instead of std::optional with an std::variant?
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. ...
0
votes
1
answer
110
views
What does this variant storage use the buffer array for?
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(...
3
votes
1
answer
196
views
Substituting `std::holds_alternative` in C++14 with mapbox variant
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::...
0
votes
1
answer
125
views
Runtime type info for accessing std::variant data
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>...
4
votes
1
answer
311
views
How to assign to a std::variant with duplicate types?
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 ...
2
votes
0
answers
41
views
Weird behavior dealing with c++ std::variant comparison and operator functions [duplicate]
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 ...
2
votes
1
answer
105
views
unexpected results in selecting conversion operator overload? [duplicate]
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 ...
0
votes
0
answers
67
views
How to performantly submit messages of arbitrary size (and fields) to a common interface?
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,
...
2
votes
1
answer
105
views
how to write an overloaded with ignore feature while visit a variant
Code:
#include <iostream>
#include <string>
#include <vector>
#include <variant>
struct ignore
{
template <typename T>
void operator()([[maybe_unused]]const T&...
0
votes
1
answer
141
views
How to initialize member std::variant variable based on the input of the constructor
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 ...
0
votes
0
answers
81
views
C++17 std::visit perfomance vs dynamic inheritance [duplicate]
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 ...
1
vote
3
answers
127
views
vector containing both string and string_view using std::variant
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>>...
5
votes
1
answer
296
views
Register types for std::variant programatically
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 ...
1
vote
1
answer
529
views
Why can't std::variant types be classes with an explicit copy constructor?
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 ...
2
votes
1
answer
102
views
Program with std::variant works in msvc but not in gcc
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&...
-1
votes
2
answers
200
views
Is there an option to create a vector that contains functions with different return types?
I have the following vectors
std::vector<std::pair<std::string, std::function<std::string(const PlayerFRDb&)>>> stringExtractors = {
{" TEAM ", extractTeam},
{&...
1
vote
3
answers
153
views
How to store 3 different types in a sequential container (e.g., std::vector)
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 ...
3
votes
1
answer
89
views
How to copy between variant of type and variant of type pointer
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.
...
3
votes
1
answer
416
views
How to add a key/value pair to a map of variants in cpp
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 ...
3
votes
2
answers
202
views
Unable to update std::variant in consteval context
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 ...
0
votes
1
answer
126
views
How to store parametric, strongly typed function for a text-based command console
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 ...
5
votes
1
answer
473
views
How to delegate constructors for std::variant?
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 {};
...
3
votes
1
answer
401
views
How can I initialize this std::variant data member which has two similar alternatives?
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 ...
1
vote
1
answer
188
views
Is there a simpler way to convert a variant to another variant with const versions of the same types?
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 ...
3
votes
0
answers
167
views
Is there a bug in the Microsoft version of std::variant?
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/...
3
votes
1
answer
350
views
Is it possible to emplace a non copyable, non moveable into a std::variant [duplicate]
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&...
3
votes
1
answer
236
views
Letting compiler write operator==, <, > etc. via spaceship-operator [duplicate]
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 ==, <, ...
6
votes
2
answers
984
views
Is it possible to use std::visit on variant that hold same type more than once?
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 ...
1
vote
2
answers
789
views
How to store either std::string or std::string_view in a std::variant?
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 ...
11
votes
2
answers
7k
views
How do I tell if a std::variant holds any value at all?
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? ...
6
votes
1
answer
453
views
compiler error with std::variant - use of deleted function error
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 {
...
2
votes
1
answer
699
views
Is it okay to inherit from an std::variant in order to make it recursive?
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 ...
-2
votes
2
answers
79
views
Select & build a compile-time tuple structure based on a runtime-provided mapping
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, ...
0
votes
0
answers
89
views
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::...
0
votes
2
answers
142
views
class with std::variant - custom [] operator based on variant content
I have a simple class:
#include <variant>
struct RawDataArray{
std::variant<double*, float*> data;
template <typename T>
constexpr bool IsType() const noexcept{
...
2
votes
2
answers
797
views
std::variant behavior leads to an error when calling std::get inside std::visit: Is it intended?
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, ...
1
vote
1
answer
460
views
what is the cost of using std::visit
Consider the following code (run it)
#include <fmt/core.h>
#include <string>
#include <array>
#include <variant>
struct Student {
std::string name;
Student(const char* ...
3
votes
2
answers
813
views
How do you write a function template that determines if two arbitrary variants are holding the same type?
Consider the following function for determining if variables of identical variant types are holding the same type:
#include <variant>
#include <iostream>
template <typename... Ts>
...