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
7
votes
2 answers

boost::variant and polymorphism

I want to get pointer to base class from boost variant, if I put orignally pointer to derived class. Is there some way to achive this . The following code does not work. class A{ public: virtual ~A(){}}; class B : public A{}; typedef…
user152508
  • 3,053
  • 8
  • 38
  • 58
7
votes
1 answer

boost::variant visitor chooses the wrong overload

Why does the following print out "A boolean!"? I realise there are some weird conversion going on, since if I explicitly construct a std::string I get the correct behavior. But why does overload resolution choose visitor::operator()(bool) in the…
7
votes
3 answers

How to implement a boost::variant derived-class?

I have tried for hours to code a class deriving from boost::variant. But I do not understand what is the problem (I do not understand what the compilation error means). What are the rules to implement a clean boost::variant derived-class? #include…
oHo
  • 51,447
  • 27
  • 165
  • 200
7
votes
2 answers

Calling methods common to types in a boost::variant

If all the types in my boost::variant support the same method, is there a way to call it generically (i.e. not calling it seperately for each method of the static_visitor)? I'm trying to get something like this to work: class A { void boo()…
thehouse
  • 7,957
  • 7
  • 33
  • 32
6
votes
2 answers

What are the advantages of using std::variant as opposed to traditional polymorphic processing?

Suppose I have a Shape base class and Circle, Line, and Point derived classes. I have two functions. std::variant process(const Shape &s); Shape process(const Shape& s); I can pass in any of my derived classes and return a…
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
6
votes
1 answer

Get item by index from boost::variant like it's possible with std::variant

With std::variant I can call std::get<0>(var) to get the value in the variant as it's first type - int. How can I do this with boost::variant? boost::get<> seems to support only getting by type and not by index and I find the…
onqtam
  • 4,356
  • 2
  • 28
  • 50
6
votes
1 answer

Recursively defining and visiting a `boost::variant` using an `std::vector` containing an incomplete type - libstdc++ vs libc++

I am trying to define and visit a "recursive" boost::variant using an incomplete wrapper class and std::vector as my indirection techniques. My implementation works with libstdc++, but not with libc++. This is the way I am defining my…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
6
votes
0 answers

boost::static_visitor multivisitor non-variant arguments

Is there any inexpensive way to pass an arguments of non-variant types in addition to arguments of variant types, when multivisitor applyed? What I mean by the term "expensive way" is: #include #include #include…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
6
votes
1 answer

boost::variant - Why a template parameter has higher precedence than a const string parameter

I am witnessing a behavior in the following code that I don't understand. The point is that if I declare the second overload of operator() like either of the following: bool operator()(T other) const bool operator()(const T &other) const The output…
B Faley
  • 17,120
  • 43
  • 133
  • 223
6
votes
3 answers

Is it safe to serialize a raw boost::variant?

boost::variant claims that it is a value type. Does this mean that it's safe to simply write out the raw representation of a boost::variant and load it back later, as long as it only contains POD types? Assume that it will be reloaded by code…
bdonlan
  • 224,562
  • 31
  • 268
  • 324
5
votes
1 answer

boost::variant single storage guarantee

My goal is to guarantee single storage on all my variant types: according to 'never empty' guarantee from Boost::variant, we need to override boost::has_nothrow_copy for each bounded type. But a bit later the documentation mentions something about…
lurscher
  • 25,930
  • 29
  • 122
  • 185
5
votes
1 answer

Why does boost::spirit::qi::parse() not set this boost::variant's value?

When trying to parse text into a boost::variant, the variant's value does not get changed. The parsers by themselves appear to work fine, so my assumption is that I'm doing something wrong with the variant code. I'm using boost 1.46.1 and the…
foraidt
  • 5,519
  • 5
  • 52
  • 80
5
votes
1 answer

boost::variant recursive trouble

is there any way to make this work? I hope you'll get the idea, I'm trying to create a list by means of recursive pairs #include #include struct nil {}; typedef boost::make_recursive_variant
Voivoid
  • 461
  • 3
  • 11
5
votes
1 answer

How to simplify the plus action on boost variant?

I have boost variant type definition: typedef boost::variant< bool, int, float, double> VariantType; I want to implement add/subtract/multiply/divide action on it. Take Add class for example. The problem is if adding a new type into VariantType,…
liuzw
  • 195
  • 1
  • 1
  • 8
5
votes
1 answer

How to generalize a tree structure with variant/visitor

This is a part 2 of my question, originally posted here. Thanks to @sehe for clarifications and help. I ended up with the code that follows, but I can't figure out how can I reduce this thing to a generic solution with variant and visitor.…
1 2
3
21 22