How do I use a variable to index into a tuple using std::get<>? I have the following code:
#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;
}
and it fails with the following compiler error:
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
I actually truncated the compiler error message as I think it does not add much beyond thid point. Any idea how to make that work?
Just to clarify, using an array
type is not really an option. I have to use the tuple
cause it is the return type of an API from a third party library. The example above is just to make it easy to understand.
How do I use a variable to index into a tuple using std::get<>?
You do not, std::get<>
parameter value must be known at compile time.
Any idea how to make that work?
yes, use proper type:
int main() {
std::array<int, 2> data{ 5, 10 };
for (int i=0; i<2; i++) {
cout << "#" << i+1 << ":" << data[i] << endl;
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With