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

How do boost::variant and boost::any work?

How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in C++ don't let you use objects with constructors, destructors or overloaded…
salvador p
  • 2,905
  • 5
  • 26
  • 26
56
votes
1 answer

Boost.Any vs. Boost.Variant

I'm having trouble choosing between Boost.Any and Boost.Variant. When should I use each one? What are the advantages and disadvantages of each? I am basically looking to store some states from external sources.
the_drow
  • 18,571
  • 25
  • 126
  • 193
49
votes
1 answer

Should this code fail to compile in C++17?

I was updating a project to use C++17 and found a few instances where code that followed this pattern was causing a compile error on recent versions of clang: #include struct vis : public boost::static_visitor { void…
Jason R
  • 11,159
  • 6
  • 50
  • 81
43
votes
4 answers

Boost Variant: how to get currently held type?

As I understood all types of boost.variant are parsed into real types (meaning as if boost variant a; a="bla-bla" would after compilation turn into string a; a="bla-bla") And so I wonder: how to get what type was put into boost variant?…
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
28
votes
1 answer

best way to do variant visitation with lambdas

I want to inline visitation of variant types with lambdas. At the moment i have the following code: struct Foo { boost::variant< boost::blank , int , string , vector< int > > var; template
lurscher
  • 25,930
  • 29
  • 122
  • 185
20
votes
3 answers

What are the differences between std::variant and boost::variant?

In an answer to this SO question: What is the equivalent of boost::variant in the C++ standard library? it is mentioned that boost::variant and std::variant differ somewhat. What are the differences, as far as someone using these classes is…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
19
votes
3 answers

boost::variant - why is "const char*" converted to "bool"?

I have declared a boost::variant which accepts three types: string, bool and int. The following code is showing that my variant accepts const char* and converts it to bool. Is it a normal behavior for boost::variant to accept and convert types not…
B Faley
  • 17,120
  • 43
  • 133
  • 223
16
votes
3 answers

Why does boost::variant not provide operator !=

Given two identical boost::variant instances a and b, the expression ( a == b ) is permitted. However ( a != b ) seems to be undefined. Why is this?
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
15
votes
2 answers

Boost Variant essentially a Union in c/c++?

I'm wondering what the differences are between a Boost Variant and a union data-type in c/c++. I know that a union data-type takes up the same memory location and the largest data type in the region of memory occupies the total amount of memory used…
pandoragami
  • 5,387
  • 15
  • 68
  • 116
14
votes
1 answer

How does boost::variant store references?

The following code compiles and does the "right thing" : #include #include int main() { int a = 10; boost::variant x = a; a = 20; std::cout << boost::get(x) << "\n"; return 0; } How does…
keveman
  • 8,427
  • 1
  • 38
  • 46
14
votes
4 answers

How to achieve dynamic polymorphism (run-time call dispatch) on unrelated types?

GOAL: I would like to achieve type-safe dynamic polymorphism (i.e. run-time dispatch of a function call) on unrelated types - i.e. on types which do not have a common base class. It seems to me that this is achievable, or at least theoretically…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
13
votes
3 answers

What is the equivalent of boost::variant in the C++ standard library?

I am looking for an alternative to C-style union. boost::variant is one such option. Is there anything in std C++ ? union { int i; double d; }
cached
  • 569
  • 1
  • 7
  • 14
12
votes
1 answer

Iterating over the types in a boost::variant

I'm using a boost variant to hold some generated types, right now my code generator creates a header with the types and a variant capable of holding them. At initialization time, I'd like to iterate over the allowable types in the variant, not the…
swarfrat
  • 486
  • 1
  • 6
  • 13
12
votes
3 answers

boost::static_visitor with multiple arguments

typedef boost::variant Type; class Append: public boost::static_visitor<> { public: void operator()(int) {} void operator()(double) {} }; Type type(1.2); Visitor visitor; boost::apply_visitor(visitor, type); Is it…
Baz
  • 12,713
  • 38
  • 145
  • 268
11
votes
2 answers

how to increase the number of types that can handled by boost::variant

I am designing a parser for verilog language, and one of the rule have 25 components, which I need a large boost::variant to hold it: typedef boost::variant< shared_ptr , …
shengyushen
  • 289
  • 3
  • 13
1
2 3
21 22