Нагуглил вот такой рабочий кусок кода http://stackoverflow.com/a/8993311
Код
#include <iostream>
#include <tuple>
template<std::size_t> struct int2type{};
template<class Tuple, std::size_t I>
void print_imp(Tuple const& t, int2type<I>){
print_imp(t, int2type<I-1>());
std::cout << ' ' << std::get<I>(t);
}
template<class Tuple>
void print_imp(Tuple const& t, int2type<0>){
std::cout << std::get<0>(t);
}
template<class Tuple>
void print(Tuple const& t){
static std::size_t const size = std::tuple_size<Tuple>::value;
print_imp(t, int2type<size-1>());
}
#include <tuple>
template<std::size_t> struct int2type{};
template<class Tuple, std::size_t I>
void print_imp(Tuple const& t, int2type<I>){
print_imp(t, int2type<I-1>());
std::cout << ' ' << std::get<I>(t);
}
template<class Tuple>
void print_imp(Tuple const& t, int2type<0>){
std::cout << std::get<0>(t);
}
template<class Tuple>
void print(Tuple const& t){
static std::size_t const size = std::tuple_size<Tuple>::value;
print_imp(t, int2type<size-1>());
}
В первый вызов print_imp() в качестве второго аргумента передается экземпляр int2type с шаблонным параметром size-1.
Код
print_imp(t, int2type<size-1>());
Соответственно попадаем сюда
Код
template<class Tuple, std::size_t I>
void print_imp(Tuple const& t, int2type<I>){
print_imp(t, int2type<I-1>());
std::cout << ' ' << std::get<I>(t);
}
void print_imp(Tuple const& t, int2type<I>){
print_imp(t, int2type<I-1>());
std::cout << ' ' << std::get<I>(t);
}
КАК из того переданного экземляра int2type мы получили сохраненное там ранее I??????????
Я бы понял, если бы реализация int2type была более "классической", как у Александреску в библиотеке Loki, где внутри класса int2type есть static с сохраненным значением, но тут просто какая-то магия!!!