Snippet, also on godbolt
#include <tuple>
#include <utility>
template <typename Tuple, std::size_t... Ints>
auto select_tuple(Tuple&& tuple, std::index_sequence<Ints...>) {
return std::tuple<std::tuple_element_t<Ints, Tuple>...>(
std::get<Ints>(std::forward<Tuple>(tuple))...);
}
int main() {
std::tuple<int, char, float> t{1, 'x', 2.0};
auto t2 = select_tuple(t, std::index_sequence<0, 2>{});
}
As you can see in the godbolt link, the compiler throws an error:
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/bits/utility.h:135:5: error: implicit instantiation of undefined template 'std::tuple_element<0, std::tuple<int, char, float> &>'
135 | using tuple_element_t = typename tuple_element<__i, _Tp>::type;
Compiler thinks it does not know std::tuple_element
, but tuple
is included.
What is wrong here?
You actually have std::tuple_element_t<0, std::tuple<int, char, float>&>
(notice the reference),
you need to remove reference (and cv qualifier) in traits:
template <typename Tuple, std::size_t... Ints>
auto select_tuple(Tuple&& tuple, std::index_sequence<Ints...>) {
return std::tuple<std::tuple_element_t<Ints, std::decay_t<Tuple>>...>(
std::get<Ints>(std::forward<Tuple>(tuple))...);
}