Questions tagged [boost-variant]

Boost.Variant is a C++ library containing a safe, generic, stack-based discriminated union container, offering a simple solution for manipulating an object from a heterogeneous set of types in a uniform manner.

Boost.Variant is a C++ library containing a safe, generic, stack-based discriminated union container, offering a simple solution for manipulating an object from a heterogeneous set of types in a uniform manner.

Whereas standard containers such as std::vector may be thought of as "multi-value, single type," boost::variant is "multi-type, single value."

317 questions
4
votes
1 answer

Iterator for boost::variant

Hy there, I'm trying to adapt an existing code to boost::variant. The idea is to use boost::variant for a heterogeneous vector. The problem is that the rest of the code use iterators to access the elements of the vector. Is there a way to use the…
Ivan
  • 19,560
  • 31
  • 97
  • 141
4
votes
1 answer

C++: Nested map

Here is the definition: struct nmap; struct nmap: map>{}; The last line below doesn't work: nmap my_map; my_map["a"] = "b"; my_map["c"] = new nmap; my_map["c"]["d"] = "e"; What do I need to add, in order…
user76568
  • 456
  • 4
  • 16
4
votes
1 answer

C++ Mutually Recursive Variant Type

I am trying to represent a PDF object type in c++ using variants. A PDF object is one of the following: Boolean Integer Real String Name Stream Array Map As you can see, the Object type is mutually recursive because the…
Ell
  • 4,238
  • 6
  • 34
  • 60
4
votes
1 answer

boost::optional reference with boost::variant type

I'm currently writing some code for a game and part of that involves creating a history of the actions that have taken place so far in the game. This history is stored in a vector of state_pair_t's pairs of actions (action_t's) and pointers to the…
shuttle87
  • 15,466
  • 11
  • 77
  • 106
4
votes
1 answer

Changing boost::variant underlying type from visitor

I have a recursive variant that models an S-expression: struct sexpr { typedef boost::variant< nil, int, double, symbol, string, boost::recursive_wrapper > >…
Haitham Gad
  • 1,529
  • 2
  • 13
  • 23
4
votes
1 answer

String parser with boost variant recursive wrapper

The code below (adapted from spirit qi mini_xml example) does not compile. There is an error related to the rule brac that has an attribute of an recursive boost::variant. However, all commented out versions of brac do compile. I am very curious…
pdug
  • 45
  • 3
4
votes
2 answers

boost::variant linker error with gcc

I am going a bit nuts trying to figure out why the following won't compile: #include #include #include #include typedef unsigned long long very_long; typedef boost::variant< int, std::string >…
user396404
  • 2,759
  • 7
  • 31
  • 42
3
votes
1 answer

"Cannot convert parameter" using boost::variant iterator

I want to create a function that can take different types of iterators which store the same type of object: The first is a std::map containing shared_ptr (typedef-ed as FooMap) and the other is a std::list which also contains shared_ptr
foraidt
  • 5,519
  • 5
  • 52
  • 80
3
votes
2 answers

How does boost::variant allow string constants?

So I've been playing around with typelists and boy are they interesting. One of things I wanted to do was attempt to implement my own variant class simply as an experiment in education on how typelists work and how they can be useful. Here's what my…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
3
votes
1 answer

Define boost variant type to pass explicitly empty values

I want to have boost::variant with empty state. So I define a boost::variant with boost::blank as the first alternative. But then I want to pass this as function parameter: void f(Variant v); ... void g() { f(boost::blank{}); } It does not look…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
3
votes
1 answer

Why are there two variant class implementations in Boost?

Boost seems to have two implementations of a variant class template: boost::variant boost::variant2 It is rare (though not unheard of) for Boost two include two takes on the same concept. Why has this happened for variants? How do these variants…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
2 answers

How to decide on stack vs heap vs boost::pool allocation in a case like this?

I have a class that uses boost::variant to store a double or a string, like this : class value { boost::variant val; }; It's supposed to be an immutable value type for a toy interpreter I'm playing with. At first it seemed…
fingerprint211b
  • 1,176
  • 12
  • 24
3
votes
1 answer

`constructor required before non-static data member` - Am I hitting c++ core issue 1360 with a `boost::variant`?

With this code #include #include struct Outer { struct StateA { std::size_t index = std::string::npos; }; struct StateB {}; using State = boost::variant; struct Inner …
wreckgar23
  • 1,025
  • 9
  • 22
3
votes
1 answer

Why can't I visit this custom type with boost::variant?

The following code: #include #include #include struct A { A() { } ~A() throw() { } A& operator=(A const & rhs) { return *this; } bool operator==(A const &…
Ken Smith
  • 775
  • 5
  • 11
3
votes
2 answers

Why does Boost Variant use the template constructor instead of the move constructor for boost::beast::websocket::stream?

I am trying to wrap boost::beast::websocket::stream (for 2 specific T) in a boost::variant to allow treating TLS ([T = boost::asio::ssl::stream]) and non-TLS ([T = boost::asio::ip::tcp::socket]) websockets the same.…
Chris Hunt
  • 3,840
  • 3
  • 30
  • 46