With the following code how do I write the custom template deduction correctly?
template<class R, class State, class... Ts> struct visitor : Ts... { using Ts::operator()...; };
template<class R, class State, class... Ts> visitor(State, Ts...)->visitor<class R, State,Ts...>;
using Event = std::variant<HeartBeat, ConfigurationRead>;
using State = std::variant<Idle, Starting, Running, Error>;
void case3()
{
Event e;
State currentState;
State newState = std::visit( visitor{
[](Idle&& state, HeartBeat event) {std::cout << "Currently stopped, heartbeat received.\n"; return Error{}; }
}, currentState, e);
}
I have looked at several examples, but I cannot find one which uses the return of the std::visit.
Your visitor does not need to deduce and encode the return type
R
-- one will be inferred bystd::visit
itself: deduced from the callable or fixed to a specified template argument (see below). That being said, it could be simplified to:In c++17 each visitor must, however, return the same type. You could avoid any type mismatch by, e.g., explicitly specifying one for each lambda expression:
DEMO
In c++20, the return type can be specified as the first template argument of
std::visit
: