The boost.variant library and boost::make_recursive_variant

  • Thread starter =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki
  • Start date
?

=?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

We have this code:
typedef boost::make_recursive_variant<
int
>::type int_tree_t;
Use of the resultant variant type is as expected:
std::vector< int_tree_t > subresult;
subresult.push_back(3);
subresult.push_back(5);

std::vector< int_tree_t > result;
result.push_back(1);
result.push_back(subresult);
result.push_back(7);

int_tree_t var(result);
According to documentation in library of boost.variant, a variable var
it's ( 1 ( 3 5 ) 7 ).
Queastion:Who do it print in language C++ by next of std::cout?
Thanks in advance.
 
D

dasjotre

We have this code:
typedef boost::make_recursive_variant<
int

Use of the resultant variant type is as expected:
std::vector< int_tree_t > subresult;
subresult.push_back(3);
subresult.push_back(5);

std::vector< int_tree_t > result;
result.push_back(1);
result.push_back(subresult);
result.push_back(7);

int_tree_t var(result);
According to documentation in library of boost.variant, a variable var
it's ( 1 ( 3 5 ) 7 ).
Queastion:Who do it print in language C++ by next of std::cout?
Thanks in advance.

We have this code:
typedef boost::make_recursive_variant<
int

Use of the resultant variant type is as expected:
std::vector< int_tree_t > subresult;
subresult.push_back(3);
subresult.push_back(5);

std::vector< int_tree_t > result;
result.push_back(1);
result.push_back(subresult);
result.push_back(7);

int_tree_t var(result);
According to documentation in library of boost.variant, a variable var
it's ( 1 ( 3 5 ) 7 ).
Queastion:Who do it print in language C++ by next of std::cout?
Thanks in advance.

(I suppose the question is HOW to print var to get ( 1 ( 3 5 ) 7 ) )

you provide visitor with function operator overloaded for all
the types your variant can hold.

struct print_visitor : public boost::static_visitor<void>
{
// print int
void operator()(int i) const
{
std::cout << i << ' ';
}
// print vector of int_tree_t
void operator()(std::vector<int_tree_t> const & v) const
{
std::cout << " ( ";
for(std::size_t i=0; i<v.size(); ++i )
boost::apply_visitor(print_visitor(), v);

std::cout << " ) ";
}
};

boost::apply_visitor(print_visitor(), var);

regards

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top