gpt4 book ai didi

C++ std::get<变量> 失败

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:47 25 4
gpt4 key购买 nike

如何通过 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;
}

它因以下编译器错误而失败:

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 (int i=0; i<2; i++) {
^
prog.cpp:10:46: error: no matching function for call to
'get(std::tuple<int, int>&)'
cout << "#" << i+1 << ":" << get<i>(data) << endl;
^
prog.cpp:10:46: note: candidates are:
In file included from /usr/include/c++/4.9/tuple:38:0,
from prog.cpp:2:
/usr/include/c++/4.9/utility:143:5: note: template<unsigned int _Int,
class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int,
std::pair<_Tp1, _Tp2> >::type& std::get(std::pair<_Tp1, _Tp2>&)
get(std::pair<_Tp1, _Tp2>& __in) noexcept
^
/usr/include/c++/4.9/utility:143:5: note: template argument
deduction/substitution failed:
prog.cpp:10:46: 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 (int i=0; i<2; i++) {
^
prog.cpp:10:46: note: in template argument for type 'unsigned int'
cout << "#" << i+1 << ":" << get<i>(data) << endl;
^
In file included from /usr/include/c++/4.9/tuple:38:0,
from prog.cpp:2:
/usr/include/c++/4.9/utility:148:5: note: template<unsigned int _Int,
class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int,
std::pair<_Tp1, _Tp2> >::type&& std::get(std::pair<_Tp1, _Tp2>&&)
get(std::pair<_Tp1, _Tp2>&& __in) noexcept
^
/usr/include/c++/4.9/utility:148:5: note: template argument
deduction/substitution failed:
prog.cpp:10:46: 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 (int i=0; i<2; i++) {
^
prog.cpp:10:46: note: in template argument for type 'unsigned int'
cout << "#" << i+1 << ":" << get<i>(data) << endl;
^
In file included from /usr/include/c++/4.9/tuple:38:0,
from prog.cpp:2:
/usr/include/c++/4.9/utility:153:5: note: template<unsigned int _Int,
class _Tp1, class _Tp2> constexpr const typename
std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(const
std::pair<_Tp1, _Tp2>&)
get(const std::pair<_Tp1, _Tp2>& __in) noexcept
^
/usr/include/c++/4.9/utility:153:5: note: template argument
deduction/substitution failed:
prog.cpp:10:46: 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 (int i=0; i<2; i++) {
^
prog.cpp:10:46: note: in template argument for type 'unsigned int'
cout << "#" << i+1 << ":" << get<i>(data) << endl;
^
In file included from /usr/include/c++/4.9/tuple:38:0,
from prog.cpp:2:
/usr/include/c++/4.9/utility:162:5: note: template<class _Tp, class
_Up> constexpr _Tp& std::get(std::pair<_T1, _T2>&)
get(pair<_Tp, _Up>& __p) noexcept

我实际上截断了编译器错误消息,因为我认为它不会增加太多超出这一点的内容。知道如何让它发挥作用吗?

编辑:

澄清一下,使用 array 类型并不是一个真正的选择。我必须使用 tuple 因为它是来自第三方库的 API 的返回类型。上面的例子只是为了方便理解。

最佳答案

How do I use a variable to index into a tuple using std::get<>?

你没有,std::get<>参数值必须在编译时已知。

Any idea how to make that work?

是的,使用正确的类型:

int main() {
std::array<int, 2> data{ 5, 10 };
for (int i=0; i<2; i++) {
cout << "#" << i+1 << ":" << data[i] << endl;
}
return 0;
}

关于C++ std::get<变量> 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com