我有以下代码,它与Visual Studio 2017编译但不与GCC 7编译:
#include <variant>
#include <iostream>
#include <array>
void print(int in) { std::cout << "int: " << in << std::endl; }
void print(double in) { std::cout << "double: " << in << std::endl; }
template <typename... Ts>
void print(std::variant<Ts...>& data, int type_idx) {
using function_type = void (*)(std::variant<Ts...>&);
constexpr auto size = sizeof...(Ts);
constexpr std::array<function_type, size> funcs = {
[](std::variant<Ts...>& data2) {
data2 = Ts(); // default construct it so we can get it later
print(std::get<Ts>(data2));
}...};
funcs[type_idx](data, val);
}
int main() {
print<int, double>(1);
}
错误:参数包未使用“...”展开:
任何想法如何使它工作?关键是不要扩展lambda中的Ts
包,而是使用我们正在扩展的当前类型