How to make a safer C++ variant visitor, similar to switch statements?
If you want to only allow a subset of types, then you can use a static_assert at the beginning of the lambda, e.g.: template <typename T, typename… Args> struct is_one_of: std::disjunction<std::is_same<std::decay_t<T>, Args>…> {}; std::visit([](auto&& arg) { static_assert(is_one_of<decltype(arg), int, long, double, std::string>{}, “Non matching type.”); using T = std::decay_t<decltype(arg)>; if constexpr (std::is_same_v<T, int>) std::cout << “int … Read more