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
Questions tagged [std-variant]
139 questions
2
votes
2 answers
Is there std::variant and std::visit way to replace old legacy dispatch for messy code?
There is an old legacy code which can not be modified. If simplified it looks like this:
enum class Type {A, B, C};
struct Object {Type type;};
Object* objs[N];
int count = 0;
#define addobj(ptr) objs[count++] = (Object*)ptr
struct A {
Type…

Rvd Boston
- 21
- 2
2
votes
1 answer
Call std::visit with passed lambdas
I have a struct that contains a variant.
I want to write a member function for that struct that should run code depending on which type variant currently holds.
However, I have issues making it compile.
I don't want to use more "template…

Raildex
- 3,406
- 1
- 18
- 42
2
votes
4 answers
How to filter out elements of certain data types in a vector of std::variant?
I have a std::vector of std::variant elements with type int or std::set. I want to loop over this vector and insert an extra item if the iterated element is of type std::set. However, it seems that querying the index at run-time is not…

livemyaerodream
- 890
- 6
- 19
2
votes
1 answer
C++ - Stream/cout for a variant of the form >
I have tried every snippet I can find on streaming the contents of a variant but nothing seems to work in my particular use case:
Given a variant of the form std::variant> myVar how can one go about streaming the contents…

rab
- 23
- 4
2
votes
1 answer
Expressing a Python class as a type in std::variant in C++ using pybind11
The working example below returns, a vector of type variant consisting of float and int64_t, to Python. The intent (illustrated by the commented lines of code **) is to add further functionality to this by enabling a Decimal (python class),…

dreadblenks
- 95
- 7
2
votes
0 answers
How does the "overloaded" trick for calling visit on variants in C++ actually work?
I've seen code like the following popping up in C++17 codebases and have started using it myself:
template
struct overloaded : Ts... {
using Ts::operator()...;
};
template
overloaded(Ts...)->overloaded;
int…

jwezorek
- 8,592
- 1
- 29
- 46
2
votes
2 answers
How to access the type inside a std::variant?
I need to build a vector of a class that can have multiple type like this:
#include
#include
#include "Field.h"
using namespace std;
int main()
{
variant v;
vector , Field,…

Hoàng Thái Vương
- 31
- 5
2
votes
2 answers
Use of emplace function of std::variant in template
I am experimenting with finate state machines and std::variant<..>
std::variant will hold all possible states The states will be defined in State classes.
I'd like to have std::monostate as the first type of State variant to use it as a "do not…

hreintke
- 51
- 4
2
votes
2 answers
How do you make the use of std::variants more "palatable", syntax-wise?
This is motivated by an answer I gave a newbie user, where I suggested they use an std::variant instead of a union.
With a union, you may have something like the following:
struct Box {
struct Item { float value; };
using Boxes =…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
1 answer
Transitioning Boost Spirit parser from boost::variant to std::variant
I'm currently trying to move some code away from using boost::variant in favour of std::variant, but have run into a problem that I can't figure out. Below is a minimal test case:
#include
#include
#include…

Tim Angus
- 983
- 11
- 26
2
votes
1 answer
How to return a different type based on a enum argument
I have two functions which require the following:
Function 1: Requires the address of a variable to set the value. ( It knows about the correct type)
Function 2: Is a overloaded function which requires the value of a type.
I need a way to return a…

yehi
- 21
- 3
2
votes
0 answers
boost::mpi and boost:serialization with std::variant
c++17 introduces the new type std::variant. Is it possible to define a serialization routine, so as to use std::variant in conjunction with boost::mpi?
Consider, e.g., a simple program
#include
#include
#include…

francesco
- 7,189
- 7
- 22
- 49
2
votes
2 answers
boost::polycollection, std::variant, or CRTP?
Suppose the "standard" C++ inheritance paradigm:
struct GeneralFunc
{
/*..members..*/
virtual double value(double a, double b) { return 0; }
};
struct Func_classA : GeneralFunc
{
/*..members..*/
double value(double a, double b) { return a *…

Christopher Mauney
- 459
- 1
- 5
- 10
1
vote
2 answers
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 to a small piece of the full source code (which,…

kteperin
- 11
- 3
1
vote
2 answers
Implementation of a polymorphic [] operator for accessing a std::variant
Suppose I have a struct MyMap, which is a wrapper for std::map where value type is std::variant but A and B behave completely different (they have different member functions and fields, and share no common functionality).
For example:
#include…

TheMemeMachine
- 187
- 9