I am trying to simulate the std::visit function in c++17 (gcc7.1)
template<typename ... Ts>
void myvisit(variant<Ts...> v)
{
size_t N = std::variant_size<decltype(v)>::value;
for (int i=0;i<N;++i) //try to iterate variadic pack
{
using T = decltype(get<i>(v));
if (holds_alternative<T>(v))
{
cout<< get<T>(v);
break;
}
}
}
int main()
{
using VAR = variant<int, string, double>;
vector<VAR> vv;
vv.push_back("app");
vv.push_back(123);
vv.push_back(1.02);
for(auto v:vv)
myvisit(v);
return 0;
}
Here I get an issue that:
error: the value of ‘i’ is not usable in a constant expression
using T = decltype(get < i >(v));
I am wondering how to solve it?