I'd like to use std::variant to process variant type but come up against some problems.
#include <string>
#include <vector>
#include <tuple>
#include <variant>
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...)->overloaded<Ts...>;
void test_variant()
{
using A = int;
using B = double;
using C = std::string;
using var_t = std::variant<A, B, C>;
using var_pair_t = std::tuple<var_t, var_t>;
std::vector<var_pair_t> vec;
for (var_pair_t const& var_pair : vec)
std::visit(overloaded{
[](std::tuple<A, A> const& pair_A) {},
[](std::tuple<B, B> const& pair_B) {},
[](std::tuple<C, C> const& pair_C) {},
[](auto const& arg) {},
}, var_pair);
}
On GCC: https://gcc.godbolt.org/z/p1ljQv
On VS2017 the compiler error:
C2672: 'function': no matching overloaded function found
C2783: 'declaration' : could not deduce template argument for 'identifier'
I want to handle the pair of the same type. What I am doing wrong? I suppose that pairs with different types would match auto const& arg, so all pair should match correctly.