I have a problem with a variant value to an overloaded function. I want to call the overloaded function with an int or string depending on what is stored in the variant. This is how I want to do that, but i can't :
class X
{
void foo(int i, int z) { /*use int i and z*/; }
void foo(const std::string& s, int z) { /*use string s and z*/; }
struct MyVisitor : public boost::static_visitor<int>
// !!! Here is the problem.
// I can't return int or std::string,
// so it's impossible to use template operator()
{
template<typename Data>
const Data operator()(const Data data) const { return data; }
};
public:
/*somehow m_queue pushed ...*/
void func_uses_variant(int z)
{
boost::variant<int, std::string> v = m_queue.pop();
foo(boost::apply_visitor(MyVisitor(), v), z);
}
private:
SomeQueue m_queue;
}
Is it possible to write it using visitor or should I do something like this:
void func_uses_variant(int z)
{
boost::variant<int, std::string> v = m_queue.pop();
if (int* foo_arg = boost::get<int>(&v))
{
foo(*foo_arg, z);
}
else if (std::string* foo_arg = boost::get<std::string>(&v))
{
foo(*foo_arg, z);
}
}
I tried to to use variadics for MyVisitor but failed because of boost::static_visitor interface. Maybe there is a solution for that.
int z in function is just to show that there is not just boost::variant in the foo() parameters.