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
5
votes
3 answers

How to get currently held variant type, and define new variables of that type

I have a boost::variant of types such as: typedef boost::variant< uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float, double, std::string > StorageTt; A StorageTt variable, say val, is set to one of these storage types…
Kevin
  • 16,549
  • 8
  • 60
  • 74
5
votes
2 answers

Nesting boost variant types to increase the type limit?

Consider: typedef boost::variant variant_T_t; typedef boost::variant variant_U_t; ... typedef boost::variant variant_t; This extends the limit on the number of types that can be held by my…
jwalk
  • 1,120
  • 11
  • 27
4
votes
1 answer

How do I make a recursive boost::variant which works with gcc 4.6?

I am decoding bencode, and have some code which works well with gcc 4.4. But having recently upgraded to gcc 4.6 this code no longer builds: #ifndef BENCODE_VALUETYPES_H #define BENCODE_VALUETYPES_H #include #include…
Kristian
  • 6,357
  • 4
  • 36
  • 37
4
votes
1 answer

How to use comparison operators on variant with contained types?

I'm using variant a lot in my code and I need to make comparisons with the content in some places to test the content of the variant for its value. For example: if(equals(aVariant, 0)){ //Something } else { //Something else } with…
Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
4
votes
1 answer

Using mpl::vector to define boost::variant types

I'm using the library boost::variant to store a large number of types. As the number of type is growing, I will soon reach the limit of 20 types. In the documentation it seems possible to define the variant using a mpl::vector, which allows more…
neodelphi
  • 2,706
  • 1
  • 15
  • 22
4
votes
1 answer

Boost-spirit-karma and boost-variant "concepts" related to auto generators

I need to deserialize a std::vector> with decoration supplied by other objects. One of the things the "decoration" enables is a empty entry in the vector. I have hit a brick wall in my real implementation. However, I have managed…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
4
votes
2 answers

Assign variant from variant?

Using = does not work. I have code like this, but it is a "bit" ugly. #include #include #include #include using namespace std; namespace detail { template void…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
4
votes
3 answers

How to pass `boost::static_visitor` instances to functions

I'm using boost::variant quite often in my projects. My colleagues now came up with the idea to pass around instances of specific boost::static_visitor in order to customize the type of visitation. She had some code like the following: #include…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
4
votes
0 answers

Boost variant recursive wrapper with non-copyable type

I am trying to create a variant of non-copyable types possibly using the recursive wrapper, such as in the following code: #include "boost/variant.hpp" using namespace std; using namespace boost; struct A { A() {} A(A &&) noexcept {} A&…
Mirco
  • 99
  • 7
4
votes
1 answer

boost::variant with immovable types

I have a type T that does not support movement: struct T { T(); T(T const&) = delete; T& operator=(T const&) = delete; T(T&&) = delete; T& operator=(T&&) = delete; }; How do I create an object of type boost::variant? The…
user1804599
4
votes
3 answers

Determining largest sizeof() in boost variant

Given: boost::variant Calculate the following at compile time: max(sizeof(T1), sizeof(T2), sizeof(T3),... ,sizeof(TN)) I had no idea how to approach this, but this answer shed some light on how I might get started. Using the code…
Matt K
  • 598
  • 5
  • 19
4
votes
1 answer

How operator<< with boost::variant is implemented

I understand that boost::variant is implemented something like so template struct variant { std::aligned_union::type buffer; .... }; How can we make an operator<< for a struct like this that prints the casts the type…
Curious
  • 20,870
  • 8
  • 61
  • 146
4
votes
1 answer

boost variant over several possibilities

Let's say I have a template class over one size a one type: template class C {}; I want to generate a boost::variant which is capable of holding this class over several sizes and types, e.g. for sizes 1 and 2 and types int and…
Dvir Yitzchaki
  • 487
  • 4
  • 13
4
votes
1 answer

parsers written with g++/bison/boost::variant compile very slow

I have written a verilog parser with bison, and use boost::variant to store all the difference cases for each variant of each rules. I use a small example, the BNF rule of expression, to show my data structure: expression : primary | expression…
shengyushen
  • 289
  • 3
  • 13
4
votes
2 answers

Boost variant of references and equality comparison

The following program aborts: #include using variant_type = boost::variant< int&, std::string& >; int main () { int a, b; variant_type v (a), u (b); v == u; return 0; } with: $ g++ -std=c++14 t.cpp && ./a.out…
Engineerist
  • 367
  • 2
  • 13