Feb 22, 2011 at 1:45pm Feb 22, 2011 at 1:45pm UTC
Hi everyone,
I've recently started using boost libraries and im facing a problem.
I have the following boost::variant typedef:
typedef boost::variant<std::string,float ,int ,bool > Value;
I need Value to hold a vector of Values itself, can this be done?
A person I know told me it could be done using recursive_wrapper but it seems i'm not getting it right nor if it can actually be done. I tried to compile the following code and receive several invalid template arguments error because Value has not yet been declared:
typedef boost::variant<std::string,float ,int ,bool , std::vector< boost::recursive_wrapper<Value> > > Value;
Could anyone tell me if this can be done and if possible what am I doing wrong?
Thanks in advance! :)
Feb 22, 2011 at 5:19pm Feb 22, 2011 at 5:19pm UTC
Thank you jsmith it actually works like that!
It brought me a lot of new troubles because of the use I gave to the old variant but that's got nothing to do with this.
Thanks again!
Cya
Feb 23, 2011 at 9:49pm Feb 23, 2011 at 9:49pm UTC
I tried to initialize an instance of the Value, but I always get a compilation error.
Also I have the following code with boost pointer and would like also to initialize,
1 2 3 4
struct nil { };
typedef boost::make_recursive_variant<nil,
std::pair<int , boost::scoped_ptr<boost::recursive_variant_> > >::type Value;
Could anyone help me?
(I would like to have a simple example of initialization)
Thanks in advance!
Last edited on Feb 24, 2011 at 3:22pm Feb 24, 2011 at 3:22pm UTC
Feb 24, 2011 at 6:38pm Feb 24, 2011 at 6:38pm UTC
I did myself the first with no pointers.
If it might help someone, I put the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
typedef boost::make_recursive_variant< std::string, float , int , bool ,
std::vector< boost::recursive_variant_ > >::type Value;
std::vector< Value > subresult;
subresult.push_back(("hello" , 0, 3, true , ("2nd" , 2)));
subresult.push_back(5);
std::vector< Value> result;
result.push_back(1);
result.push_back(subresult);
result.push_back(7);
Value var(result);
It compiles.
To be clear, it represents the resultant content of var as
( 1 (hello 0 3 true (2nd 2) 5 ) 7 ).
To print everything:
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
struct print_visitor : public boost::static_visitor<void >
{
// print int
void operator ()(int i) const
{
std::cout << i << ' ' ;
}
// print vector of Value
void operator ()(std::vector<Value> const & v) const
{
std::cout << " ( " ;
for (std::size_t i=0; i<v.size(); ++i )
boost::apply_visitor(print_visitor(), v[i]);
std::cout << " ) " ;
}
void operator ()(float i) const
{
std::cout << i << ' ' ;
}
void operator ()(bool i) const
{
std::cout << i << ' ' ;
}
void operator ()(std::string i) const
{
std::cout << i << ' ' ;
}
};
boost::apply_visitor(print_visitor(), var); // output: ( 1 ( 2 5 ) 7 )
1 2 3 4 5 6 7 8 9 10
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <boost\variant\recursive_wrapper.hpp>
#include <boost\make_shared.hpp>
#include <boost\variant\recursive_variant.hpp>
#include <boost\variant\variant.hpp>
#include <utility>
#include <boost\visit_each.hpp>
#include <boost/variant/apply_visitor.hpp>
With scoped_ptr pointer it is trivial.
Last edited on Feb 24, 2011 at 8:12pm Feb 24, 2011 at 8:12pm UTC
Feb 24, 2011 at 8:15pm Feb 24, 2011 at 8:15pm UTC
To finish with:
Note: Portability is unfortunately, due to standard conformance issues in several compilers, make_recursive_variant is not universally supported.
On these compilers the library indicates its lack of support via the definition of the preprocessor symbol BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT. Thus, unless working with highly-conformant compilers, maximum portability will be achieved by instead using recursive_wrapper.
Feb 24, 2011 at 8:36pm Feb 24, 2011 at 8:36pm UTC
Yes. Boost has become a good stress test for compilers :)