
I spent quite some time figuring this out and 2 minutes after I sent this e-mail I discovered the obvious solution... variant::types exposes the types of the variant as a MPL vector, which can be used to statically extract the vector index of the type visited. sorry for the pointless mail. Am Wednesday 18 November 2009 21:00:55 schrieben Sie:
Hi,
I have a problem regarding the static visitor of boost::variant.
there is a variant and a mpl vector:
variant
var; typedef mpl::vector 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
::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
struct A_holder{ typedef B B_type; A value; }; variant ,A_holder ,A_holder > 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?