Visual Studio 2017 为“std::get”添加了红色下划线,因此程序无法编译。我该怎么做才能让它发挥作用?
一个片段:
std::tuple<int, int, int>t;
t = {1,1,1};
int a = 0;
int b = std::get<a>(t);
最佳答案
模板参数在编译时计算。您可以使用 constexpr
修复错误,使编译器能够在编译时计算 a
的值。
std::tuple<int, int, int> t;
t = {1, 1, 1};
constexpr int a = 0;
int b = std::get<a>(t);
或者,如果您的元组只包含一种类型的元素,您可以将其替换为 std::array
或 std::vector
(或任何类似的容器)并在运行时执行下标。
std::array<int, 3> array;
t = {1, 1, 1};
int a = 0;
int b = array[a];
关于c++ - 为什么基于 int 的访问对 std::get(std::tuple) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921113/