C ++ std :: get <variable> doesn't work

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

      

Actually I was truncating the compiler error message as I think it doesn't add much beyond that point. Any idea how to make this work?

Edit:

Just to clarify, using a type array

is not really an option. I have to use tuple

because this is the return type of the API from a third party library. The above example just makes it easy to understand.

+3


source to share


4 answers


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

You don't know, the value of the parameter std::get<>

must be known at compile time.

Any idea how to make this work?



yes, use the correct 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;
}

      

+4


source


Any idea how to make this work?

Option 1

Use compile-time constants to access std::tuple

.

cout << "#" << 1 << ":" << get<0>(data) << endl;
cout << "#" << 2 << ":" << get<1>(data) << endl;

      

Option 2



Use a container type whose items can be accessed using an index at run time.

std::vector<int> data{5, 10};

      

or

std::array<int, 2> data{5, 10};

      

+3


source


The likely answer you should accept is to simply use an indexed container with an array, vector, or whatever.


In case the elements of a tuple are not of uniform types and you actually need an answer for this case, it is a little tricky. This is because the types must be known at compile time. So when you think you can do std::cout << get_from_tuple(a_tuple, index)

, for example, it may not work as easily as you think, because the overload operator<<

for sending the object to standard output is chosen at compile time. Obviously, this means that the index must also be known at compile time, otherwise we cannot know the element type of the tuple.

However, it is possible to build a template function that can actually implement exactly this behavior. The final compilation output is a conditional tree that can handle every element in the tuple, but we are borrowing the compiler to help us build that conditional tree.

What I will be building here is a function that, given a tuple, index and functor, will invoke a functor passing that particular tuple element and return true. If the index is out of range, it will return false.

If the functor cannot be called with every element in the tuple, the template function cannot instantiate.

The final solution looks like this:

#include <tuple>
#include <type_traits>

namespace detail {
    template <std::size_t I>
    struct size_wrapper { };

    template <typename V, typename Tup, std::size_t I>
    bool visit_tuple_index_impl(Tup && t, std::size_t index, V && visitor, size_wrapper<I>)
    {
        if (index == I - 1) {
            visitor(std::get<I - 1>(std::forward<Tup>(t)));
            return true;
        }

        return visit_tuple_index_impl(std::forward<Tup>(t), index, visitor, size_wrapper<I - 1>());
    }

    template <typename V, typename Tup>
    bool visit_tuple_index_impl(Tup &&, std::size_t, V &&, size_wrapper<0>)
    {
        return false;
    }
}

template <typename V, typename Tup>
bool visit_tuple_index(Tup && t, std::size_t index, V && visitor)
{
    return detail::visit_tuple_index_impl(
        std::forward<Tup>(t),
        index,
        std::forward<V>(visitor),
        detail::size_wrapper<std::tuple_size<typename std::decay<Tup>::type>::value>()
    );
}

      

0


source


#include <utility>

template<std::size_t...Is>
auto index_over( std::index_sequence<Is...> ) {
  return [](auto&& f)->decltype(auto){
    return decltype(f)(f)( std::integral_constant<std::size_t, Is>{}... );
  };
}
template<std::size_t N>
auto index_upto( std::integral_constant<std::size_t, N> ={} ) {
  return index_over( std::make_index_sequence<N>{} );
}
template<class F>
auto foreacher( F&& f ) {
  return [f=std::forward<F>(f)](auto&&...args)mutable {
    (void(), ..., void(f(decltype(args)(args))));
  };
}
template<std::size_t N>
auto count_upto( std::integral_constant<std::size_t, N> ={} ) {
  return [](auto&& f){
    index_upto<N>()(foreacher(decltype(f)(f)));
  };
}

      

you can simply do:

#include <iostream>
#include <tuple>

int main() {
  std::tuple<int, int> data(5, 10);
  count_upto<2>()([&](auto I){
    std::cout << "#" << (I+1) << ":" << std::get<I>(data) << "\n";
  });
}

      

A living example .

There is no unlimited recursion in this solution. This requires C ++ 1z - you can replace the body with a foreacher

trick using unused=int[];

in C ++ 14.

0


source







All Articles