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

Using boost::variant and getting a generic return type

I am looking for ideas of how to refactor this code or opinions on whether or not I'm over-thinking it. Consider the following boost::variant using Pattern = boost::variant; Now here is the idea of what I'd like to…
Addy
  • 2,414
  • 1
  • 23
  • 43
0
votes
1 answer

use boost::variant to call template class functions

I have a template class template class class1 { template< typename T1, typename T2> void func1 () { // do somthing } template< typename T1> void func2 () { //do something } …
Dodez
  • 19
  • 2
0
votes
2 answers

Using std::vector> with Google Mock causes a compilation error

There is this code: #include "gmock/gmock.h" #include typedef boost::variant, std::vector> CustomVariant; // some overloads which don't work std::ostream& operator<<( std::ostream& stream, …
wulujey
  • 384
  • 1
  • 2
  • 9
0
votes
1 answer

Boost recursive variant along with template c++

I am trying to understand boost recursive variant and template. For the below code, I get compilation error of cannot convert argument 1 from 'std::vector>' to 'const…
legameeternoforall
  • 69
  • 1
  • 2
  • 10
0
votes
2 answers

boost::variant and polymorphism in c++11

I am experimenting with polymorphism and boost::variant in c++11 Here is the code #include #include using namespace std; class Polygon { protected: int width, height; public: void set_values (int a,…
cateof
  • 6,608
  • 25
  • 79
  • 153
0
votes
1 answer

boost::variant gives wrong result when bool appears as possible type

The code that works is the following: #include #include #include #include int main(int argc, char** argv) { std::map > values; values["a"] = 10; …
cateof
  • 6,608
  • 25
  • 79
  • 153
0
votes
1 answer

avoiding redundant code in boost::variant visitors

I am facing the following problem: I have some visitors on for boost::variant, which all doing the same for a specific type, here foo, so the method void operator()(const foo& ast) { //allways the same } is allways the same in every single…
Exagon
  • 4,798
  • 6
  • 25
  • 53
0
votes
1 answer

Utilize boost-variant to create generic factory method with boost::mpl::for_each

In one of my projects I needed to map the int of the boost::variant which()-Function to the types of the boost::variant. For some reasons the map doesn't contain the correct TVar Type? Why is that? #include #include…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
0
votes
2 answers

Deduce if type can be stored in boost::variant

I'm using boost::variant already for a while, but there are still some questions that leave me puzzled. The following code is not compiling, as I'm trying to store a std::pair to a boost::variant, that can only contain int, double and…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
0
votes
1 answer

boost::variant: strange behaviour with a recursive vector type

I have a class with a single member that is a boost::variant of two types: a vector of integers and a vector of the containing class objects (the latter obviously needs to be done with the help of recursive_wrapper). The code for the class is…
0
votes
1 answer

Accessing elements of a boost::variant vector in C++

I would like to access elements of a vector in C++. I have generated the vector using the Boost_variant library since I needed to store both int and string types as inputs. Now I would like to access elements of the vector by index, and in reverse…
Sid
  • 266
  • 5
  • 13
0
votes
0 answers

Use of boost libraries apply_visitor, bind and function in C++

I am new to the use of the boost library and have questions regarding its use. I have a vector union of ints and strings generated by use of boost::variant. Assume the vector is called myvec. I tried following the advice on this post to extract…
Sid
  • 266
  • 5
  • 13
0
votes
0 answers

Could someone explain the use of boost::visitor_ptr (boost::variant)

I would like to store function pointers in a boost::variant type. This is clear to me, including the creation and use of a visitor object. In the documentation of boost::variant I've stumbled on boost::visitor_ptr. The documentation gives the…
degski
  • 642
  • 5
  • 13
0
votes
1 answer

Why can my struct not have a member of type boost::variant but can have a member of type vector?

I have defined the following boost::variant type: #include #include #include struct SharedNodeType; typedef float TypeA; typedef int TypeB; typedef std::string TypeC; typedef char* TypeD; typedef…
stix
  • 1,140
  • 13
  • 36
0
votes
1 answer

boost::recursive_wrapper and std::unique_ptr

Currently (since C++11) it is simple to design boost::recursive_wrapper using std::unique_ptr: template< typename T > class recursive_wrapper { std::unique_ptr< T > storage; public : template< typename ...Args > recursive_wrapper(Args…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169