c++templatestuples

std::get from template argument


Could you please explain or maybe give a link that explains why this works

template <int i, int j, class T>
auto to_pair(T tuple) -> decltype(std::make_pair(std::get<i>(tuple), std::get<j>(tuple)))
{
    return std::make_pair(std::get<i>(tuple), std::get<j>(tuple));
}

and this does not?

template <class T>
auto to_pair(int i, int j, T tuple) -> decltype(std::make_pair(std::get<i>(tuple), std::get<j>(tuple)))
{
    return std::make_pair(std::get<i>(tuple), std::get<j>(tuple));
}

I mean what is the difference between int type being template argument specialization and int argument as a common argument?


Solution

  • I mean what is the difference between int type being template argument specialization and int argument as a common argument?

    One is defined at compile time while the other is defined at run time.

    A run time value cannot be used as a template parameter. Hence, the second case does not work.