Using boost::make_recursive_variant with tuple - c++

boost::make_recursive_variant is intended to make variants which are recursive, without requiring the creation of an intermediate type by using the tag type boost::recursive_variant_. For std::vector<recursive_variant_> this appears to work fine, however for a std::tuple<int, recursive_variant_> it fails.
Example:
#include <type_traits>
#include <tuple>
#include <vector>
#include <boost/variant.hpp>
using std::cout;
using std::vector;
using std::tuple;
using boost::variant;
using boost::static_visitor;
using boost::apply_visitor;
using boost::make_recursive_variant;
using boost::recursive_variant_;
using A = typename make_recursive_variant<
int,
vector<recursive_variant_>
>::type;
using B = vector<A>;
using C = typename make_recursive_variant<
int,
tuple<int, recursive_variant_>
>::type;
using D = tuple<int, C>;
int main(int, char**)
{
A a = 0;
B b{1, 2};
a = b;
C c = 0;
D d{0, 1};
//c = d;
}
If the last line is uncommented, the program no longer compiles. Is this a usage error (and if yes, how so?), or is it bug or limitation in boost::tuple?
Error message (on mingw-w64 with gcc 4.8.1):
In file included from D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant.hpp:17:0,
from D:\projects\test\main.cpp:4:
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp: In instantiation of 'void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(T&, int, mpl_::false_) [with T = const std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >; T0_ = boost::detail::variant::recursive_flag<int>; T1 = std::tuple<int, boost::recursive_variant_>; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_; mpl_::false_ = mpl_::bool_<false>]':
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp:1703:38: required from 'boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::variant(const T&) [with T = std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >; T0_ = boost::detail::variant::recursive_flag<int>; T1 = std::tuple<int, boost::recursive_variant_>; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]'
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp:2129:29: required from 'void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::assign(const T&) [with T = std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >; T0_ = boost::detail::variant::recursive_flag<int>; T1 = std::tuple<int, boost::recursive_variant_>; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]'
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp:2168:19: required from 'boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>& boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::operator=(const T&) [with T = std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >; T0_ = boost::detail::variant::recursive_flag<int>; T1 = std::tuple<int, boost::recursive_variant_>; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]'
D:\projects\test\main.cpp:37:4: required from here
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp:1557:17: error: no matching function for call to 'boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_>::initializer::initialize(void*, const std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >&)'
)
^
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp:1557:17: note: candidates are:
In file included from D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/variant.hpp:31:0,
from D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant.hpp:17,
from D:\projects\test\main.cpp:4:
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:104:24: note: static int boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::initialize(void*, boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param_T) [with BaseIndexPair = boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > > >::initializer_node, mpl_::int_<1> >; Iterator = boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> >; boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param_T = const std::tuple<int, boost::recursive_variant_>&]
static int initialize(void* dest, param_T operand)
^
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:104:24: note: no known conversion for argument 2 from 'const std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >' to 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > > >::initializer_node, mpl_::int_<1> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > >::initializer_node::param_T {aka const std::tuple<int, boost::recursive_variant_>&}'
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:115:24: note: static int boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::initialize(void*, boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param2_T) [with BaseIndexPair = boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > > >::initializer_node, mpl_::int_<1> >; Iterator = boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> >; boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param2_T = std::tuple<int, boost::recursive_variant_>&&]
static int initialize(void* dest, param2_T operand)
^
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:115:24: note: no known conversion for argument 2 from 'const std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >' to 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > > >::initializer_node, mpl_::int_<1> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > >::initializer_node::param2_T {aka std::tuple<int, boost::recursive_variant_>&&}'
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:149:17: note: static void boost::detail::variant::initializer_root::initialize()
static void initialize();
^
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:149:17: note: candidate expects 0 arguments, 2 provided
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:115:24: note: static int boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::initialize(void*, boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param2_T) [with BaseIndexPair = boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >; Iterator = boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > >; boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param2_T = int&&]
static int initialize(void* dest, param2_T operand)
^
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:115:24: note: no known conversion for argument 2 from 'const std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >' to 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > > >::initializer_node::param2_T {aka int&&}'
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:104:24: note: static int boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::initialize(void*, boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param_T) [with BaseIndexPair = boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >; Iterator = boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > >; boost::detail::variant::make_initializer_node::apply<BaseIndexPair, Iterator>::initializer_node::param_T = const int&]
static int initialize(void* dest, param_T operand)
^
D:/libs/3rdparty-prebuild-vc11-win64/vc11/win64/boost-1_54_0-vc11-win64/include/boost-1_54/boost/variant/detail/initializer.hpp:104:24: note: no known conversion for argument 2 from 'const std::tuple<int, boost::variant<boost::detail::variant::recursive_flag<int>, std::tuple<int, boost::recursive_variant_>, 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_> >' to 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, int, boost::mpl::l_item<mpl_::long_<1l>, std::tuple<int, boost::recursive_variant_>, boost::mpl::l_end> > > >::initializer_node::param_T {aka const int&}'

The problem is that make_recursive_variant isn't aware of variadic templates, so it doesn't know to expand recursive_variant_ within std::tuple<..., recursive_variant_>.
You can test this by changing std::tuple to std::pair (a fixed-adic template) and observing that that works fine.
As a workaround, you can teach Boost.Variant how to expand within variadic templates with the following snippet (place at the top level of your source file):
namespace boost { namespace detail { namespace variant {
template<template<typename...> class F, typename... Ts, typename Dest, typename Source>
struct substitute<
F<Ts...>
, Dest
, Source
BOOST_MPL_AUX_LAMBDA_ARITY_PARAM(mpl::int_<-1>)
>
{
typedef F<typename substitute<
Ts, Dest, Source
>::type...> type;
};
}}} // namespace boost::detail::variant
Edit: I've put in a pull request: https://github.com/boostorg/variant/pull/2

Related

variant pixel buffer to vector using boost::apply_visitor

I am trying to convert variablepixelbuffer.vbuffer() to a vector of double values using apply_visitor in boost.
I came with this piece of code till now :
struct GetVector : public boost::static_visitor<std::vector<double>>
{
//values will be returned in a pair. double is
// used since it can contain the value for any pixel type
typedef std::vector<double> result_type;
template<typename T
void myfunction (const T& i) { // function:
result_type.push_back(static_cast<double>(*i));
}
template<typename T>
result_type
operator() (const T& v)
{
typedef typename T::element_type::value_type value_type;
std::for_each (v->data(), v->data() + v->num_elements(), myfunction);
return result_type;
}
};
/* pixel-example-start */
void
readPixelData(const FormatReader& reader,
std::ostream& stream,int x,int y,int w,int h)
{
// Change the current series to this index
reader.setSeries(0);
// Get total number of planes (for this image index)
dimension_size_type pc = reader.getImageCount();
// Pixel buffer
VariantPixelBuffer buf;
dimension_size_type xd = x;
dimension_size_type yd = y;
dimension_size_type wd = w;
dimension_size_type hd = h;
// Loop over planes (for this image index)
for (dimension_size_type p = 0 ; p < pc; p++)
{
// Read the entire plane into the pixel buffer.
reader.openBytes(p, buf,xd,yd,wd,hd);
}
GetVector visitor;
GetVector::result_type result = boost::apply_visitor(visitor, buf.vbuffer());
}
But i am getting some long errors, and some piece of it are
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:60:43:
required from ‘typename Visitor::result_type
boost::apply_visitor(Visitor&, Visitable&) [with Visitor =
MinMaxVisitor; Visitable =
boost::variant >,
boost::mpl::v_item >,
boost::mpl::v_item
, boost::mpl::v_item >,
boost::mpl::v_item >,
boost::mpl::v_item >,
boost::mpl::v_item
, boost::mpl::v_item
, boost::mpl::v_item
, boost::mpl::v_item
, boost::mpl::v_item
, boost::mpl::vector0, 1>, 1>, 1>, 1>, 1>, 1>, 1>, 1>, 1>, 1>, 1> >, 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_,
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_, boost::detail::variant::void_>;
typename Visitor::result_type = std::vector >]’ read_subimage.C:82:85: required from here
/usr/include/c++/4.8/bits/stl_algo.h:4417:14: error: must use ‘.’ or
‘->’ to call pointer-to-member function in ‘__f (...)’, e.g. ‘(...
->* __f) (...)’
How can i reslove this, I need to get the pixel buffer data into vector .

How to implement a boost::variant derived-class?

I have tried for hours to code a class deriving from boost::variant. But I do not understand what is the problem (I do not understand what the compilation error means).
What are the rules to implement a clean boost::variant derived-class?
#include <boost/variant.hpp>
class MyVariant : public boost::variant<char,bool>
{
public:
MyVariant () : boost::variant<char,bool>( ) {}
template <typename T>
MyVariant( T& v) : boost::variant<char,bool>(v) {}
template <typename T>
MyVariant(const T& v) : boost::variant<char,bool>(v) {}
};
int main ()
{
MyVariant a;
MyVariant b = a; //compilation error
// MyVariant c = MyVariant();
// MyVariant d (true);
// MyVariant e ('E');
}
Why do I want to use inheritance? (EDIT to give more details to #zaufi)
I want an empty state
I want to accept const char* as string
I want to accept int as long
I want to give enum types
For instance, in pseudo C++ code, my hopes:
class MyVariant : public boost::variant<char,bool,long,std::string>
{
typedef boost::variant<char,bool,long,std::string> super;
public:
// I know here I should specialize templeted constructors
// but I is more clear like that, isn't it?
MyVariant() : super('e') {} //empty -> char
MyVariant(char c) : super(std::string(1,c)){} //char -> string
MyVariant(const char* s) : super(std::string(s) ) {} //char* -> string
MyVariant(int v) : super(long (v) ) {} //TODO boundaries
/* other constructors ... */
enum Type
{
NONE, //my empty state = char type
BOOL,
LONG,
STRING
};
Type type() const { return (Type) which(); }
};
The basic snippet code (on top of the question) has been tested on different platforms
boost v1.33 + GCC 4.1 (Linux)
boost v1.52 + GCC 4.7 (MinGW)
boost v1.52 + Visual C++ 2010 (v10)
Below my errors for the two versions of GCC
(I can remove one of the both if it bothers someone...)
$ g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ myVariant.cpp
/usr/include/boost/variant/variant.hpp: In constructor 'boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::variant(T&) [with T = MyVariant, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]':
myVariant.cpp:10: instantiated from 'MyVariant::MyVariant(T&) [with T = MyVariant]'
myVariant.cpp:19: instantiated from here
/usr/include/boost/variant/variant.hpp:1348: error: call of overloaded 'convert_construct(MyVariant&, long int)' is ambiguous
/usr/include/boost/variant/variant.hpp:1262: note: candidates are: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(T&, int, mpl_::false_) [with T = MyVariant, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]
/usr/include/boost/variant/variant.hpp:1321: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char, U1 = bool, U2 = boost::detail::variant::void_, U3 = boost::detail::variant::void_, U4 = boost::detail::variant::void_, U5 = boost::detail::variant::void_, U6 = boost::detail::variant::void_, U7 = boost::detail::variant::void_, U8 = boost::detail::variant::void_, U9 = boost::detail::variant::void_, U10 = boost::detail::variant::void_, U11 = boost::detail::variant::void_, U12 = boost::detail::variant::void_, U13 = boost::detail::variant::void_, U14 = boost::detail::variant::void_, U15 = boost::detail::variant::void_, U16 = boost::detail::variant::void_, U17 = boost::detail::variant::void_, U18 = boost::detail::variant::void_, U19 = boost::detail::variant::void_, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]
/usr/include/boost/variant/variant.hpp:1330: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(const boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char, U1 = bool, U2 = boost::detail::variant::void_, U3 = boost::detail::variant::void_, U4 = boost::detail::variant::void_, U5 = boost::detail::variant::void_, U6 = boost::detail::variant::void_, U7 = boost::detail::variant::void_, U8 = boost::detail::variant::void_, U9 = boost::detail::variant::void_, U10 = boost::detail::variant::void_, U11 = boost::detail::variant::void_, U12 = boost::detail::variant::void_, U13 = boost::detail::variant::void_, U14 = boost::detail::variant::void_, U15 = boost::detail::variant::void_, U16 = boost::detail::variant::void_, U17 = boost::detail::variant::void_, U18 = boost::detail::variant::void_, U19 = boost::detail::variant::void_, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]
$ g++ --version
g++.exe (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ myVariant.cpp -I /c/.../include/
In file included from c:/.../include/boost/variant.hpp:17:0,
from myVariant.cpp:1:
c:/.../include/boost/variant/variant.hpp: In instantiation of 'boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::variant(T&) [with T = MyVariant; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]':
myVariant.cpp:9:66: required from 'MyVariant::MyVariant(T&) [with T = MyVariant]'
myVariant.cpp:18:25: required from here
c:/.../include/boost/variant/variant.hpp:1406:9: error: call of overloaded convert_construct(MyVariant&, long int)' is ambiguous
c:/.../include/boost/variant/variant.hpp:1406:9: note: candidates are:
c:/.../include/boost/variant/variant.hpp:1316:10: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(T&, int, mpl_::false_) [with T = MyVariant; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_; mpl_::false_ = mpl_::bool_<false>]
c:/.../include/boost/variant/variant.hpp:1376:10: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char; U1 = bool; U2 = boost::detail::variant::void_; U3 = boost::detail::variant::void_; U4 = boost::detail::variant::void_; U5 = boost::detail::variant::void_; U6 = boost::detail::variant::void_; U7 = boost::detail::variant::void_; U8 = boost::detail::variant::void_; U9 = boost::detail::variant::void_; U10 = boost::detail::variant::void_; U11 = boost::detail::variant::void_; U12 = boost::detail::variant::void_; U13 = boost::detail::variant::void_; U14 = boost::detail::variant::void_; U15 = boost::detail::variant::void_; U16 = boost::detail::variant::void_; U17 = boost::detail::variant::void_; U18 = boost::detail::variant::void_; U19 = boost::detail::variant::void_; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]
c:/.../include/boost/variant/variant.hpp:1385:10: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(const boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char; U1 = bool; U2 = boost::detail::variant::void_; U3 = boost::detail::variant::void_; U4 = boost::detail::variant::void_; U5 = boost::detail::variant::void_; U6 = boost::detail::variant::void_; U7 = boost::detail::variant::void_; U8 = boost::detail::variant::void_; U9 = boost::detail::variant::void_; U10 = boost::detail::variant::void_; U11 = boost::detail::variant::void_; U12 = boost::detail::variant::void_; U13 = boost::detail::variant::void_; U14 = boost::detail::variant::void_; U15 = boost::detail::variant::void_; U16 = boost::detail::variant::void_; U17 = boost::detail::variant::void_; U18 = boost::detail::variant::void_; U19 = boost::detail::variant::void_; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]
The accepted answer and the other answers do not answer the question. There are good reasons to derive from boost::variant.
you want to add methods into the variant, so that it behaves more polymorphic
you want to keep using boost::apply_visitor(visitor(), variant) without jumping through hoops
Here is a working example of a type MyVariant inheriting from boost::variant. The code requires C++11 to inherit the (complex) constructors of boost::variant.
Edit: Operators such as equal comparison implemented in boost::variant do not automatically work for the derived class. This is a (unintended) consequence of how boost::variant is implemented, it explicitly prevents comparisons to "foreign" types using templates. Operators can be enabled with explicit overloads, as shown in the example.
#include<iostream>
#include<boost/variant.hpp>
struct type_size_visitor : public boost::static_visitor<unsigned> {
template <typename T>
unsigned operator()(const T&) const { return sizeof(T); }
};
template <typename... Types>
class MyVariant : public boost::variant<Types...>
{
using base_type = boost::variant<Types...>;
public:
// inherit constructors
using base_type::base_type;
// add a new method
unsigned type_size() { return boost::apply_visitor(type_size_visitor(), *this); }
/* Inheriting operators from boost::variant does not
work as it normally would. We have to add explicit
overloads for the operators we want to use.
*/
bool operator==(const MyVariant& rhs) const {
// cast rhs to base and delegate to operator in base
return base_type::operator==(static_cast<const base_type&>(rhs));
}
};
int main() {
MyVariant<std::string, char> v;
v = 1;
std::cout << v.type_size() << " " << sizeof(char) << std::endl;
v = std::string("foo");
std::cout << v.type_size() << " " << sizeof(std::string) << std::endl;
// comparison operators need workaround shown above
MyVariant<std::string, char> a, b;
a = 1; b = 1;
assert(a == b);
}
I want an empty state
boost::variant<boost::blank, bool, long, std::string>
There. That was much easier. No need for messy inheritance.
I want to give enum types
enum Type
{
NONE,
BOOL,
LONG,
STRING
};
struct GetType : public boost::static_visitor<Type>
{
Type operator()(boost::blank) {return NONE;}
Type operator()(bool) {return BOOL;}
Type operator()(long) {return LONG;}
Type operator()(const std::string&) {return STRING;}
};
//Get the type
Type t = boost::apply_visitor(GetType(), theData);
That was easy too. Plus, if you add a new type to the variant, your code will break if you don't update GetType to match.
The other two criteria require you to use a class, but you don't need inheritance. You need containment.
typedef boost::variant<boost::blank, std::string, long> VarType;
class MyVariant
{
public:
//Default construction will initialize with boost::blank.
MyVariant(char c) : m_var(std::string(1,c)) {}
MyVariant(const char* s) : m_var(std::string(s)) {}
MyVariant(int v) : m_var(long(v)) {}
MyVariant(long v) : m_var(long(v)) {}
VarType &operator *() {return m_var;}
const VarType &operator *() const {return m_var;}
private:
VarType m_var;
};
...
Type t = boost::apply_visitor(GetType(), *theData);
Finally my implementation based on Nicol Bolas's answer:
Variant.h
class Variant
{
public:
Variant() : v_(boost::blank()) {}
Variant(bool v) : v_(v) {}
Variant(long v) : v_(v) {}
Variant(int v) : v_(long(v)) {}
Variant(double v) : v_(v) {}
Variant(const std::string& s) : v_(s) {}
Variant(const char* s) : v_(std::string(s)) {}
typedef boost::variant <boost::blank, bool,
long, double, std::string> bstvar;
enum Type {
NONE, BOOL, // above underlying types
LONG, DOUBLE, STRING // and enum must be consistent:
}; // (order must be same)
operator bstvar& () { return v_; }
operator const bstvar& () const { return v_; }
bstvar & operator*() { return v_; }
bstvar const& operator*() const { return v_; }
bool empty () const { return type() == NONE; }
bool toBool () const { return boost::get<bool > (v_); }
long toLong () const { return boost::get<long > (v_); }
double toDouble () const { return boost::get<double> (v_); }
std::string const& toStr () const { return boost::get<std::string> (v_); }
std::string & toStr () { return boost::get<std::string> (v_); }
inline Type type () const { return (Type) v_.which(); }
static Type type (const std::string&);
static std::string type (Type t);
private: bstvar v_; // Data
};
Variant.cpp
#include "Variant.h"
#include <sstream>
#include <algorithm> //std::transform
Variant::Type Variant::type (const std::string& str)
{
std::string lower = str;
std::transform (lower.begin(), lower.end(), lower.begin(), ::tolower);
if (lower == "bool") return BOOL;
if (lower == "long") return LONG;
if (lower == "double") return DOUBLE;
if (lower == "string") return STRING;
else return NONE;
}
std::string Variant::type (Type t)
{
switch (t)
{
case NONE: return "none";
case BOOL: return "bool";
case LONG: return "long";
case DOUBLE: return "double";
case STRING: return "string";
default:
;//see below
}
std::ostringstream oss;
oss <<"Unexpected type="<< t;
return oss.str();
}

How to use Boost Spirit with variant with more than 20 types?

I'm parsing a quite complex grammar with Boost Spirit and I'm facing a problem with a variant that have more than 20 types (21 here):
namespace eddic { namespace ast {
typedef boost::mpl::vector<
Integer,
IntegerSuffix,
Float,
Litteral,
VariableValue,
DereferenceValue,
Expression,
Unary,
Null,
True,
False,
ArrayValue,
FunctionCall,
MemberFunctionCall,
Cast,
BuiltinOperator,
Assignment,
SuffixOperation,
PrefixOperation,
Ternary
> types_initial;
typedef boost::mpl::push_back<types_initial, New>::type types;
typedef boost::make_variant_over<types>::type Value;
}}
Boost Spirit does not recognize the last type (eddic::ast::New) added with push_back. When I parse something that have this element, it fails with this error:
eddic: /usr/include/boost/variant/detail/visitation_impl.hpp:264:
typename Visitor::result_type
boost::detail::variant::visitation_impl(int, int, Visitor&, VoidPtrCV,
mpl_::false_, NoBackupFlag, Which*, step0*) [with Which =
mpl_::int_<0>; step0 =
boost::detail::variant::visitation_impl_step,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>,
0>, 0>, 0>, 0>, 0l>,
boost::mpl::v_iter,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
boost::mpl::v_item,
0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>, 0>,
0>, 0>, 0>, 0>, 21l> >; Visitor =
boost::variant, boost::mpl::vector,
eddic::ast::Deferred,
eddic::ast::Deferred, eddic::ast::Null,
eddic::ast::True, eddic::ast::False,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred >, 0> >,
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_>::assigner; VoidPtrCV = const void*;
NoBackupFlag =
boost::variant, boost::mpl::vector,
eddic::ast::Deferred,
eddic::ast::Deferred, eddic::ast::Null,
eddic::ast::True, eddic::ast::False,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred,
eddic::ast::Deferred >, 0> >,
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_fallback_type_; typename
Visitor::result_type = void; mpl_::false_ = mpl_::bool_]:
Assertion `!"Boost.Variant internal error: 'which' out of range."'
failed.
If I swap two elements (Ternary and New for example), New is correctly recognized, but not Ternary. It is only the last element that fails.
I already tried using push_front or vector21, but it doesn't change anything, it's always the last element that have problem. In my opinion it comes from the fact that Spirit uses a variant internally before copying it to my variant_over type.
Is there a workaround to this problem ?
I could probably reduce the number to 20, but the problem is that I will certainly have more than that in the future.
Thanks a lot for any idea
Define BOOST_MPL_LIMIT_VECTOR_SIZE to whatever new limit you want, but going this high is usually a hint at design problems so it might be worth to give it some consideration.

boost::variant linker error with gcc

I am going a bit nuts trying to figure out why the following won't compile:
#include <iostream>
#include <array>
#include <boost/variant.hpp>
#include <forward_list>
typedef unsigned long long very_long;
typedef boost::variant< int, std::string > variants_type;
typedef std::array< variants_type, 5 > row_type;
typedef std::forward_list<row_type> rows_holder_type;
int main() {
rows_holder_type rows;
row_type row_data;
row_data[0] = 0;
row_data[1] = 0;
row_data[2] = 0;
row_data[3] = 0;
row_data[4] = 0;
rows.push_front(row_data);
}
This is the compiler error I am getting:
/usr/include/testing/test_code.o||In function 'std::array<boost::variant<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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_>, 5ul>::array(std::array<boost::variant<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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_>, 5ul> const&)':|
store.cpp:(.text._ZNSt5arrayIN5boost7variantIiSsNS0_6detail7variant5void_ES4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_EELm5EEC2ERKS6_[_ZNSt5arrayIN5boost7variantIiSsNS0_6detail7variant5void_ES4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_S4_EELm5EEC5ERKS6_]+0x31)||undefined reference to 'boost::variant<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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_>::variant(boost::variant<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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_> const&)'|
||=== Build finished: 1 errors, 0 warnings ===|
If I replace:
typedef boost::variant< int, std::string > variants_type;
with:
typedef boost::any variants_type;
The code compiles, but I do not want to use boost::any for performance reasons.
I tested your code with MSVC and gcc 4.7.0. MSVC compiles and links the code fine (of course with #include <string>), but gcc gives a linker error on the last line rows.push_front(row_data);. If you comment that line out, gcc accepts the code. I see no reason why gcc gives a linker error and am concluding that it is a bug with gcc. One workaround on gcc would be to change std::array to std::vector.
(From the error message I assume you are using Code::Blocks with gcc)
I've eliminated extra code and the following example still gives the linker error:
#include <boost/variant.hpp>
#include <array>
int main()
{
std::array<boost::variant<int, double>, 5> row_type1;
std::array<boost::variant<int, double>, 5> row_type2 = row_type1;
}
You're missing #include <string>; other than that your code is fine.

variant problem with compiling a file

I tried compiling the following code from this page:
http://www.pdc.kth.se/training/Talks/C++/boost/libs/variant/doc/sample.html
Under "A binary tree implementation" and I got a ton of errors that I'm having trouble with solving. I'm using gcc 3.4.5 with codeblocks.
#include <iostream>
#include "boost/variant.hpp"
#include "boost/incomplete.hpp"
using boost::variant;
using boost::incomplete;
using std::cout;
using std::endl;
struct non_leaf_node; // Forward declaration
// Define a variant with these two types:
// 1) int
// 2) The (incomplete) non_leaf_node struct
typedef variant<int, incomplete<non_leaf_node> > tree_node;
struct non_leaf_node
{
non_leaf_node(const tree_node& l, int num, const tree_node& r)
: left_(l), right_(r), num_(num) { }
non_leaf_node(const non_leaf_node& other)
: left_(other.left_), right_(other.right_), num_(other.num_) { }
int num_;
tree_node left_;
tree_node right_;
};
struct tree_printer : boost::static_visitor<void>
{
void operator()(int n) const
{
cout << n << ' ';
}
void operator()(const non_leaf_node& node) const
{
boost::apply_visitor(*this, node.left_);
cout << node.num_ << ' ';
boost::apply_visitor(*this, node.right_);
}
};
int main(int, char* [] )
{
//Build a binary search tree:
non_leaf_node a(3,4, 6);
non_leaf_node b(19, 20, 23);
non_leaf_node c(a,10, b);
tree_node root(c);
//Perform an in-order walk
boost::apply_visitor(tree_printer(), root);
return 0;
}
And the traceback during compilation:
> C:\boostvariantBtree\boostvariantBtree.cpp||In
> constructor
> `non_leaf_node::non_leaf_node(const
> tree_node&, int, const tree_node&)':|
> C:\boostvariantBtree\boostvariantBtree.cpp|24|warning:
> `non_leaf_node::right_' will be
> initialized after|
> C:\boostvariantBtree\boostvariantBtree.cpp|22|warning:
> `int non_leaf_node::num_'|
> C:\boostvariantBtree\boostvariantBtree.cpp|19|warning:
> when initialized here|
> C:\boostvariantBtree\boostvariantBtree.cpp|24|warning:
> `non_leaf_node::right_' will be
> initialized after|
> C:\boostvariantBtree\boostvariantBtree.cpp|22|warning:
> `int non_leaf_node::num_'|
> C:\boostvariantBtree\boostvariantBtree.cpp|20|warning:
> when initialized here| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp||In
> member function `typename
> Visitor::result_type
> boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&,
> int) [with T = const
> boost::incomplete<non_leaf_node>,
> Visitor = const tree_printer]':|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|128|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke_impl(int,
> Visitor&, VoidPtrCV, T*, mpl_::true_)
> [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = const
> void*, T =
> boost::incomplete<non_leaf_node>]'|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|170|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke(int,
> Visitor&, VoidPtrCV, T*, NoBackupFlag,
> int) [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = const
> void*, T =
> boost::incomplete<non_leaf_node>,
> NoBackupFlag = boost::variant<int,
> boost::incomplete<non_leaf_node>,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_, boo|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|256|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl(int,
> int, Visitor&, VoidPtrCV,
> mpl_::false_, NoBackupFlag, Which*,
> step0*) [with Which = mpl_::int_<0>,
> step0 =
> boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>,
> int,
> boost::mpl::l_item<mpl_::long_<1l>,
> boost::incomplete<non_leaf_node>,
> boost::mpl::l_end> > >,
> boost::mpl::l_iter<boost::mpl::l_end>
> >, Visitor = boost::detail::variant::invoke_visitor<const
> tree_printer>, | C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1771|instantiated
> from `static typename
> Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor_impl(int,
> int, Visitor&, VoidPtrCV) [with
> Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = const
> void*, T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::vo| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1796|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor(Visitor&)
> const [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost:| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1820|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::apply_visitor(Visitor&) const
> [with Visitor = const tree_printer,
> T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost::detail::variant::void_, T8 =
> boost::detail::vari| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\apply_visitor_unary.hpp|76|instantiated
> from `typename Visitor::result_type
> boost::apply_visitor(const Visitor&,
> Visitable&) [with Visitor =
> tree_printer, Visitable = const
> tree_node]'|
> C:\boostvariantBtree\boostvariantBtree.cpp|34|instantiated
> from here| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> no match for call to `(const
> tree_printer) (const
> boost::incomplete<non_leaf_node>&)'|
> C:\boostvariantBtree\boostvariantBtree.cpp|29|note:
> candidates are: void
> tree_printer::operator()(int) const|
> C:\boostvariantBtree\boostvariantBtree.cpp|33|note:
> void tree_printer::operator()(const
> non_leaf_node&) const| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> return-statement with a value, in
> function returning 'void'| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp||In
> member function `typename
> Visitor::result_type
> boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&,
> int) [with T =
> boost::incomplete<non_leaf_node>,
> Visitor = const tree_printer]':|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|128|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke_impl(int,
> Visitor&, VoidPtrCV, T*, mpl_::true_)
> [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = void*, T =
> boost::incomplete<non_leaf_node>]'|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|170|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke(int,
> Visitor&, VoidPtrCV, T*, NoBackupFlag,
> int) [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = void*, T =
> boost::incomplete<non_leaf_node>,
> NoBackupFlag = boost::variant<int,
> boost::incomplete<non_leaf_node>,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::de| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|256|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl(int,
> int, Visitor&, VoidPtrCV,
> mpl_::false_, NoBackupFlag, Which*,
> step0*) [with Which = mpl_::int_<0>,
> step0 =
> boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>,
> int,
> boost::mpl::l_item<mpl_::long_<1l>,
> boost::incomplete<non_leaf_node>,
> boost::mpl::l_end> > >,
> boost::mpl::l_iter<boost::mpl::l_end>
> >, Visitor = boost::detail::variant::invoke_visitor<const
> tree_printer>, | C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1771|instantiated
> from `static typename
> Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor_impl(int,
> int, Visitor&, VoidPtrCV) [with
> Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = void*, T0_
> = int, T1 = boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1785|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor(Visitor&)
> [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost::detai| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1810|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::apply_visitor(Visitor&) [with
> Visitor = const tree_printer, T0_ =
> int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost::detail::variant::void_, T8 =
> boost::detail::variant::v| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\apply_visitor_unary.hpp|76|instantiated
> from `typename Visitor::result_type
> boost::apply_visitor(const Visitor&,
> Visitable&) [with Visitor =
> tree_printer, Visitable = tree_node]'|
> C:\boostvariantBtree\boostvariantBtree.cpp|49|instantiated
> from here| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> no match for call to `(const
> tree_printer)
> (boost::incomplete<non_leaf_node>&)'|
> C:\boostvariantBtree\boostvariantBtree.cpp|29|note:
> candidates are: void
> tree_printer::operator()(int) const|
> C:\boostvariantBtree\boostvariantBtree.cpp|33|note:
> void tree_printer::operator()(const
> non_leaf_node&) const| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> return-statement with a value, in
> function returning 'void'| ||=== Build
> finished: 4 errors, 6 warnings ===|
As far as I see, current boost seems not to have incomplete.hpp.
If your boost is recent one,
by removing the line
#include "boost/incomplete.hpp" and using boost::incomplete;,
and replacing the line
typedef variant<int, incomplete<non_leaf_node> > tree_node;
with
typedef variant<int, boost::recursive_wrapper<non_leaf_node> > tree_node;
probably the code will be compiled.
Here is a test on ideone.
If you need a recursive type,
this document
Recursive variant types
might help.