Could you please help me figure out why is this not working i.e. refering to the comment in the code //I need to do this but I can't. I thought this the goal!
? I have no idea why this is not working, it's inspired by examples I have seen online.
#include <variant>
#include <iostream>
template<typename... args>
class Visitor //: public boost_base_visitor<double>...
{
public:
virtual ~Visitor() = default;
virtual double visit(typename std::variant<args...> visitable)
{
auto op = [this](typename std::variant<args...> visitable) -> double { return this->apply(visitable); };
return std::visit(std::ref(op), visitable);
}
virtual double apply(typename std::variant<args...> visitable) = 0;
Visitor() = default;
};
class SubVisitor : public Visitor<std::string, double>
{
public:
virtual ~SubVisitor() = default;
SubVisitor() : Visitor<std::string, double>() {};
virtual double apply(std::variant<std::string, double> visitable) override
{
//return process(visitable); //I need to do this but I can't. I thought this the goal!
return process(std::get<std::string>(visitable)); //I DON'T KNOW IF THIS IS REALLY A STRING??
};
virtual double process(std::string visitable)
{
std::cout << "STRING HANDLED" << std::endl;
return 0.0;
}
virtual double process(double visitable)
{
std::cout << "DOUBLE HANDLED" << std::endl;
return 1.0;
}
};
int main(int argc, char* argv[])
{
SubVisitor v;
v.apply("dd");
//v.apply(1.0); //This will fail as we only handle string?? What is the purpose of variant then?
return 1;
}
I am getting error when uncommenting the process
function above:
Error C2664: 'double SubVisitor::process(std::string)': cannot convert argument 1 from 'std::variantstd::string,double' to 'std::string'