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
10
votes
1 answer

Construct a boost variant containing a value of the nth-type in the variant type index?

I want to construct boost::variants containing default-constructed values, specified with a type index - without writing my own switch statement over the type index. I figure this must be possible, somehow, with MPL? To clarify though, the index is…
James
  • 24,676
  • 13
  • 84
  • 130
10
votes
3 answers

visitor template for boost::variant

I would like to use a boost.variant as a parameter to a template 'Visitor' class which would provide visitor operators as required by the boost.variant visitor mechanism, in this case all returning void i.e., void operator()(T0…
Tom Jordan
  • 101
  • 1
  • 1
  • 3
10
votes
0 answers

ASTs: prefer inheritance or variants?

In object oriented languages, it is fairly common to implement ASTs (Abstract Syntax Trees) using simple hierarchies (the Composite pattern), and to traverse them via visitors. In functional programming languages, using variants/sum types, and then…
akim
  • 8,255
  • 3
  • 44
  • 60
9
votes
1 answer

Is there boost::visit like std::visit, for boost::variant?

With C++14, I'm using boost::variant as a way of compile-time polymorphism: using MyType = boost::variant; Both classes have a method sayHello(). I'd like to call: MyType obj = ...; // either A() or B() boost::visit([](auto&& o) {…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
9
votes
1 answer

How to define heterogenous std::map using boost::variant in "two dimensional manner"

I would be happy to get and advice how to deal with boost::variant in "two dimensional manner". Sounds strange but let my code say more (hopefully): I have coded a class called Parameter: template
Martin Kopecký
  • 912
  • 5
  • 16
9
votes
1 answer

boost::variant implementation

I'm just curious about boost::variant's implementation. Does it work like this? Two members: A number representing the currently stored type (i.e. 0 for the first template parameter, 1 for the second template parameter etc) A union of all…
Clinton
  • 22,361
  • 15
  • 67
  • 163
8
votes
4 answers

boost::variant usage

I am developing GUI application via wxWidgets. It has 2 parts: GUI part and "Logic" part. I want to have Logic part totally independent on wxWidgets. But one component in the GUI returning wxVariant and I need to use it in the Logic part. So I am…
relaxxx
  • 7,566
  • 8
  • 37
  • 64
8
votes
1 answer

boost variant simple call to common methods

I have two pointers that only one of them can be set, so I am considering using boost::variant, say: boost::variant shared_ptr>. Type 1 and 2 are different but they share some functionality. Thay for example, both have the…
gsf
  • 6,612
  • 7
  • 35
  • 64
8
votes
2 answers

Boost variant visitor with an extra parameter

I have code that resembles below. typedef uint32_t IntType; typedef IntType IntValue; typedef boost::variant MsgValue; MsgValue v; Instead of saying this, IntValue value = boost::apply_visitor(d_string_int_visitor(), v); I…
Ivan
  • 7,448
  • 14
  • 69
  • 134
8
votes
1 answer

Boost variant ambiguous construction

The Boost Variant documentation says the following of the constructor that accepts arbitrary type: template variant(T & operand); Requires: T must be unambiguously convertible to one of the bounded types (i.e., T1, T2, etc.). The same…
petersohn
  • 11,292
  • 13
  • 61
  • 98
8
votes
2 answers

boost::variant; std::unique_ptr and copy

This Question Determined That a Non-Copyable Type Can't Be Used With Boost Variant Tree class template class Tree{ private: class TreeNode{ public: std::unique_ptr Nodes Move…
Mushy
  • 2,535
  • 10
  • 33
  • 54
7
votes
1 answer

Boost Variant: How to model JSON?

I'm trying to parse JSON string using Boost Spirit store JSON object into recursive data structures: Value <== [null, bool, long, double, std::string, Array, Object]; Array <== [Value, Value, Value, ...]; Object <== ["name1": Value, "name2": Value,…
Viet
  • 17,944
  • 33
  • 103
  • 135
7
votes
2 answers

C++ Mutually Recursive Variant Type (Again)

I have a problem similar to that described here: C++ Mutually Recursive Variant Type I am trying to create a JSON representation in C++. Many libraries already offer excellent JSON representations and parsers that are very fast, but I am not…
rg6
  • 329
  • 2
  • 10
7
votes
2 answers

Creating a new boost-variant type from given nested boost-variant type

Assume that I have a nested boost::variant-type TNested containing some types and some other boost::variant types (that itself cannot contain again boost::variant types, so that there will be no recursion). I'm looking for a template alias…
7
votes
1 answer

How to check if a template type is one of the types of a variant type?

Considering a variant type and a template function, how can I check the template type is one of the types of the variant ? Is there a more elegant way than the following ? typedef boost::variant Var; template void f(const T&…
Caduchon
  • 4,574
  • 4
  • 26
  • 67
1
2
3
21 22