Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@miguelmartin75
Created August 25, 2014 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmartin75/c8335ff0af327dc5e5b4 to your computer and use it in GitHub Desktop.
Save miguelmartin75/c8335ff0af327dc5e5b4 to your computer and use it in GitHub Desktop.
basically converts std::tuple<T1, T2, ..., TN> to std::tuple<X<T1>, X<T2>, ..., X<TN>>, hue.
#include <iostream>
#include <tuple>
template<template <typename T> class X>
struct Wrap
{
template <typename T, typename... Args>
struct Deducer
{
typedef decltype(std::tuple_cat(std::tuple<X<T>>(), typename Deducer<Args...>::tuple())) tuple;
};
template<typename T>
struct Deducer<T>
{
typedef std::tuple<X<T>> tuple;
};
template<typename... Args>
using tuple = typename Deducer<Args...>::tuple;
};
// BECAUSE RTTI IS A SHIT CUNT
template <typename T>
struct MyType
{
const char* name() { return "generic type"; }
};
#define NAME_MY_TYPE(x) template<> struct MyType<x> \
{ \
const char* name() { return "MyType<"#x">"; } \
}
NAME_MY_TYPE(int);
NAME_MY_TYPE(double);
NAME_MY_TYPE(char);
template<std::size_t I = 0, typename... Tp>
typename std::enable_if<I == sizeof...(Tp), void>::type
print(std::tuple<Tp...>& t)
{ }
template<std::size_t I = 0, typename... Tp>
typename std::enable_if<I < sizeof...(Tp), void>::type
print(std::tuple<Tp...>& t)
{
std::cout << std::get<I>(t).name() << std::endl;
print<I + 1, Tp...>(t);
}
int main(int argc, char *argv[])
{
Wrap<MyType>::tuple<int, double, char> x;
print(x);
return 0;
}
/**
* Expected output:
* MyType<int>
* MyType<double>
* MyType<char>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment