I am confusing the following problem by identifying the a specific type within boost::variant and pass it as member function argument within a class object. Consider the following code
typedef boost::variant<int, string, double> Variant;
class test{
void func1 (Variant V);
void func2 (string s);
void func3 ();
};
test::func1(Variant V){
// How can I identify the type of V inside the body of this function?
Or how can I call the apply_visitor inside this function.
/*
if(v.type() == string)
func2();
else if(v.type() == double)
funct3();
*/
}
int test::func2(){ cout << "func3" << endl;}
int test::func3(){ cout << "func4" << endl;}
...
int main ()
{
test t;
Variant V = 3;
t.func1(V);
V = "hello";
t.func1(V);
}
I thought sure about implementing a visitation class/structure (apply_visitor ) within class test. However I got stuck by calling a outer member function namely func3(string s) from the overloaded operator implemented in the visitor class.
The visitor pattern reification will make the compiler do the type-checking for you. All you need to do is tell the compiler what to do when a
stringis in theVariant:(check out the example at
http://www.boost.org/doc/libs/1_35_0/doc/html/variant/tutorial.html)
and use
boost::apply_visitorto choose the correct function:The
my_dispatcher(&t)will create an object of your static_visitor implementation, that will be used by theapply_visitormagic.Hope that’s what your were looking fore, since your question wasn’t really clear.
Note: alternatively, you derive your
testfromstatic_visitor.