64,685
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <tuple>
#include <string>
using namespace std;
template<typename Tuple, int N = std::tuple_size<Tuple>::value>
struct Printer
{
static void log(Tuple& t) {
Printer<Tuple, N - 1>::log(t);
using type = typename std::tuple_element<N - 1, Tuple>::type;
std::string ts = typeid(type).name();
type& v = std::get<N - 1>(t);
std::cout << ts << ":" << v << std::endl;
}
};
template<typename Tuple>
struct Printer<Tuple, 1>
{
static void log(Tuple& t) {
using type = typename std::tuple_element<0, Tuple>::type;
std::string ts = typeid(type).name();
type& v = std::get<0>(t);
std::cout << ts << ":" << v << std::endl;
}
};
int main() {
std::tuple<int, bool, string> t = std::forward_as_tuple(11, true, "ok");
Printer<std::tuple<int, bool, string>>::log(t);
return 1;
}