Heterogeneous container with boost

Hi. I'm trying to create an heterogeneous container with boost libraries, but I get a compilation error.
Here it's my minimal example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <boost/variant.hpp>

using namespace std;
using namespace boost;

template <class T>
class B
{
	T value;

	public:
	B(T v) { value = v; };
	T getValue() { return value; }
};

typedef variant<B<int>, B<float>, B<double>, B<string> > var_t;

class print_visitor : boost::static_visitor<>
{
	public:
		template<typename T>
			void operator()(T & x) const
			{
				cout << "Print something" << endl;
			}
};

class A
{
	vector<var_t> vec;

	public:
	void addObj(var_t obj) { vec.push_back(obj); }
	var_t getObj(int i) { return vec.at(i); }
};

int main(int argc, char* argv[])
{
	A *container = new A();

	B<int>    elem1(10);

	container->addObj(elem1);

	apply_visitor( print_visitor(), container->getObj(0) );

	return 0;
}

And here the error:
heterogeneousContainerBoost.cpp: In function ‘int main(int, char**)’:
heterogeneousContainerBoost.cpp:52: error: no matching function for call to ‘apply_visitor(print_visitor, var_t)’
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:54: note: candidates are: typename Visitor::result_type boost::apply_visitor(Visitor&, Visitable&) [with Visitor = print_visitor, Visitable = var_t]
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:70: note:                 typename Visitor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = print_visitor, Visitable = var_t]

Could anyone help me to understand where am I wrong?

Thanks in advance.
The error is saying that the second argument of apply_visitor should be a reference, you are returning from getObj a temporary value.
Aside from that, a design question:

I'm not sure what the purpose of B is? It seems to be a useless wrapper at this point. Why not just a
typedef boost::variant<int, float, double, string> var_t?
This is a simplified structure of a bigger program, I need to create a vector of template objects and not a vector with normal types.
For that reason I have created B.

So Bazzy, can you give me an example on how obtain my object as a reference?
It's a very simple fix:
var_t& getObj(int i) { return vec.at(i); }
     ^ that                               
Making that fix you will get tons of compilation errors...
up
You need to inherit boost::static_visitor<> publicly.
Solved. Thak you guys.
Here the working version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <boost/variant.hpp>

using namespace std;
using namespace boost;

template <class T>
class B
{
	T value;

	public:
	B(T v) { value = v; };
	T getValue() { return value; }
};

typedef variant<B<int>, B<float>, B<double>, B<string> > var_t;

class print_visitor : public boost::static_visitor<>
{
	public:
		template<typename T>
			void operator()(T & x) const
			{
				cout << "Value = " << x.getValue() << endl;
			}
};

class A
{
	vector<var_t> vec;

	public:
	void addObj(var_t obj) { vec.push_back(obj); }
	var_t& getObj(int i) { return vec.at(i); }
};

int main(int argc, char* argv[])
{
	A *container = new A();

	B<int>    elem1(10);
	B<float>  elem2(15.7);
	B<string> elem3("Test");

	container->addObj(elem1);
	container->addObj(elem2);
	container->addObj(elem3);

	apply_visitor( print_visitor(), container->getObj(0) );
	apply_visitor( print_visitor(), container->getObj(1) );
	apply_visitor( print_visitor(), container->getObj(2) );

	return 0;
}


and its output:
Value = 10
Value = 15.7
Value = Test
Topic archived. No new replies allowed.