Is it safe to modify a variant inside the visitor function?
struct visitor {
visitor(std::variant<int, std::string> & var) : var(var){}
void operator()(int i) {
var = std::to_string(i);
}
void operator()(const std::string & s) {
var = atoi(s.c_str());
}
std::variant<int, std::string> & var;
};
void convert(std::variant<int, std::string> & var) {
std::visit(visitor{var}, var);
}
In the string reference function in the example above, I would assume that the string reference is valid until the assignment of the new value. But does the standard say anything about the validity of this use case?