Can somebody explain in simple terms the below template code syntax which I found while learning C++ 17 std::visit
.
#include <iostream>
#include <string>
#include <variant>
template<typename... Targs>
struct VariantType : Targs... {
using Targs::operator()...;
};
template<typename... Targs>
VariantType(Targs...) -> VariantType<Targs...>;
int main() {
std::variant<int, double, std::string> var;
var = "Hello, world!";
std::visit(
VariantType {
[] (int i) { std::cout << "int: " << i << std::endl; },
[] (double d) { std::cout << "double: " << d << std::endl; },
[] (const std::string& str) { std::cout << "std::string: " << str << std::endl; }
}, var
);
}