|
Boost Users : |
Subject: [Boost-users] [variant] apply_visitor/static access to which()
From: Stefan Strasser (strasser_at_[hidden])
Date: 2009-11-18 15:00:55
Hi,
I have a problem regarding the static visitor of boost::variant.
there is a variant and a mpl vector:
variant<A1,A2,A3> var;
typedef mpl::vector<B1,B2,B3> B_types;
the variant and the vector have the same number of types, but not the same
types.
I need to "visit" the element of the variant, and access the B_types element
at the same position as the element of the variant:
struct visitor : static_visitor<void>{
template<class A>
void operator()(A a) const{
typedef typename mpl::at_c<B_types,var.which()>::type B; //error
}
};
so if var.which() == 1, I need a typedef of type B2. obviously the code above
doesn't work because which() is a runtime function. but the value of which()
is known statically at this point (that's the point of visitation).
is there a way to access it, with boost::variant visitors?
I like neither of the solutions I can come up with:
1. write my own visitation pattern that not only instantiates the visitor with
the variant type but also with the vector type.
2.
convert the variant to
template<class A,class B>
struct A_holder{
typedef B B_type;
A value;
};
variant<A_holder<A1,B1>,A_holder<A2,B2>,A_holder<A3,B3> > var;
3. do a seperate "visition" of the mpl vector inside the variant visitor:
struct visitor : static_visitor<void>{
template<class A>
void operator()(A a) const{
switch(var.which()){
case...
case...
}
}
};
is there a better way to do this?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net