Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implicit instantiation of undefined template std::tuple_element

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))...);
}

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading