Reputation: 465
How can I make this recursive variant work? I'd like to have a container of variant, or container of container of variant.
template <class T>
class A
{
// ...
T t_;
};
template <class T>
class AVariant
{
typedef typename boost::make_variant_over<T>::type Type;
Type t;
// ...
};
template <class Container>
class Composite
{
typedef typename Container::value_type T;
Container container_of_any_;
// ....
};
In main.cpp:
typedef AVariant<boost::mpl::list<
A<int>,
A<long>,
boost::recursive_wrapper<Composite<std::vector<Any> > > // Compile error: 'Any' is not declared in this scope
> > Any;
I understand that Compoiste > is not right, but not sure how?
Edit: I've changed the following:
template <class T>
class AVariant
{
typedef typename boost::make_recursive_variant_over<T>::type Type;
Type t_;
// ...
template <class Archive>
class SerializeVisitor : public boost::static_visitor<>
{
SerializeVisitor( Archive& ar ) : ar_(ar) {}
template <typename U>
void operator()( const U& t ) const
{
ar_ & BOOST_SERIALIZATION_NVP( t );
}
};
friend class boost::serialization::access;
template<class Archive>
void serialize( Archive& ar, const unsigned int version )
{
boost::apply_visitor( SerializeVisitor<Archive>( ar ), t_ );
}
};
Then instantiate using:
typedef AVariant<boost::mpl::list<
A<double>,
A<int>,
std::vector<boost::recursive_variant_> // to make the recursion slightly simplier
> > Any;
And my AVariant has a visitor to serialize as above. It still gives compile error when serialization is invoked in main(). It seems to look for serialize() function for the class std::vector in the error:
const class std::vector<boost::variant<boost::detail::variant::recursive_flag<boost::detail::variant::over_sequence<boost::mpl::vector<A<double>, A<long int>, A, std::vector<boost::recursive_variant_, std::allocator<boost::recursive_variant_> >, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> > >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, std::allocator<boost::variant<boost::detail::variant::recursive_flag<boost::detail::variant::over_sequence<boost::mpl::vector<A<double>, A<long int>, A, std::vector<boost::recursive_variant_, std::allocator<boost::recursive_variant_> >, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> > >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > >' has no member named 'serialize'
Upvotes: 0
Views: 600
Reputation:
You need to use boost::make_recursive_variant
instead of boost::recursive_wrapper
with boost::recursive_variant_
in it.So you should try this:
typedef AVariant<boost::mpl::list<
A<int>,
A<long>,
boost::make_recursive_variant<Composite<std::vector<boost::recursive_variant_> >
>::type > > Any;
This can help you.
You should refer here: http://www.boost.org/doc/libs/1_54_0/doc/html/variant/tutorial.html#variant.tutorial.recursive.recursive-variant
and here http://www.boost.org/doc/libs/1_54_0/boost/variant/recursive_variant.hpp for more information about recursive variants.
If you want to serialize variant you should better use boost::variant
visitation mechanism to write out the actual type contained in the variant:
variant_serializer ser;
boost::apply_visitor( ser, your_variant);
Upvotes: 1