2 Lines Of Code and 3 C++17 Features - The overload Pattern

1 · Bartłomiej Filipek · Feb. 11, 2019, 7:37 a.m.
While I was doing research for my book and blog posts about C++17 several times I stumbled upon this pattern for visitation of std::variant: template<class... Ts> struct overload : Ts... { using Ts::operator()...; };template<class... Ts> overload(Ts...) -> overload<Ts...>;std::variant<int, float> intFloat { 0.0f };std::visit(overload( [](const int& i) { ... }, [](const float& f) { ... }, ), intFloat;); With the above pattern, you can provide separate lambdas “in-place” for visitation. I...