小编rag*_*nar的帖子

C++ std :: get <variable>失败

如何使用变量使用std :: get <>索引到元组?我有以下代码:

#include <iostream>
#include <tuple>
using namespace std;

int main() {
  tuple<int, int> data(5, 10);
  for (int i=0; i<2; i++) {
    cout << "#" << i+1 << ":" << get<i>(data) << endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它失败并出现以下编译器错误:

prog.cpp: In function 'int main()':
prog.cpp:10:39: error: the value of 'i' is not usable in a constant expression
      cout << "#" << i+1 << ":" << get<i>(data) << endl;
                                       ^
prog.cpp:9:11: note: 'int i' is not const
  for …
Run Code Online (Sandbox Code Playgroud)

c++ stdtuple

5
推荐指数
2
解决办法
2471
查看次数

标签 统计

c++ ×1

stdtuple ×1