Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::get<variable> fails

Tags:

c++

stdtuple

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?

Edit:

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.

like image 480
ragnar Avatar asked Apr 25 '17 21:04

ragnar


1 Answers

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;
}
like image 119
Slava Avatar answered Oct 13 '22 18:10

Slava