Skip to content

Instantly share code, notes, and snippets.

@niha
Created September 16, 2010 05:46
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 niha/582026 to your computer and use it in GitHub Desktop.
Save niha/582026 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
char* demangle(const char *demangle) {
int status;
return abi::__cxa_demangle(demangle, 0, 0, &status);
}
template <typename... seq>
struct array{};
template <typename ary>
struct head;
template <typename first, typename... rest>
struct head<array<first, rest...>> {
typedef first type;
};
template <typename lhs, typename rhs>
struct append;
template <typename... lhs, typename... rhs>
struct append<array<lhs...>, array<rhs...>> {
typedef array<lhs..., rhs...> type;
};
template <typename ary, int n>
struct split_at;
template <typename ary>
struct split_at<ary, 0> {
typedef array<> front;
typedef ary back;
};
template <typename head, typename... tail>
struct split_at<array<head, tail...>, 1> {
typedef array<head> front;
typedef array<tail...> back;
};
template <typename head, typename second, typename... tail>
struct split_at<array<head, second, tail...>, 2> {
typedef array<head, second> front;
typedef array<tail...> back;
};
template <typename... seq, int n>
struct split_at<array<seq...>, n> {
static const int lhs_len = n / 2;
static const int rhs_len = n - lhs_len;
typedef split_at<array<seq...>, lhs_len> lhs;
typedef typename lhs::front lhs_front;
typedef typename lhs::back lhs_back;
typedef split_at<lhs_back, rhs_len> rhs;
typedef typename rhs::front rhs_front;
typedef typename rhs::back rhs_back;
typedef typename append<lhs_front, rhs_front>::type front;
typedef rhs_back back;
};
template <typename T, int size>
struct fill;
template <typename T>
struct fill<T, 0> {
typedef array<> type;
};
template <typename T>
struct fill<T, 1> {
typedef array<T> type;
};
template <typename T, int size>
struct fill {
static const int lhs_size = size / 2;
static const int rhs_size = size - lhs_size;
typedef typename fill<T, lhs_size>::type lhs;
typedef typename fill<T, rhs_size>::type rhs;
typedef typename append<lhs, rhs>::type type;
};
typedef
typename fill<int, 1200>::type
tmp;
typedef
typename append<tmp, array<double>>::type
tmp_;
typedef
typename split_at<tmp_, 1200>::front
front;
typedef
typename split_at<tmp_, 1200>::back
back;
typedef
typename head<back>::type
should_double;
int main(){
should_double d = 1.0;
std::cout << demangle(typeid(should_double).name()) << std::endl;
std::cout << demangle(typeid(back).name()) << std::endl;
printf("%lf\n", d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment