Name: Anonymous 2011-08-05 4:03
//
// prog_cudder.hpp
//
#ifndef PROG_CUDDER_HPP
#define PROG_CUDDER_HPP
#include <cstddef>
#include <tuple>
namespace prog
{
namespace detail
{
template <class... List>
struct cudder;
template <>
struct cudder<std::nullptr_t>
{
typedef std::nullptr_t car_type;
typedef std::nullptr_t cdr_type;
};
template <class Tail>
struct cudder<Tail>
{
typedef Tail car_type;
typedef std::nullptr_t cdr_type;
};
template <class Tail>
struct cudder<std::tuple<Tail>>
{
typedef Tail car_type;
typedef std::nullptr_t cdr_type;
};
template <class Car, class... Cdr>
struct cudder<Car, Cdr...>
{
typedef Car car_type;
typedef std::tuple<Cdr...> cdr_type;
};
template <class Car, class... Cdr>
struct cudder<std::tuple<Car, Cdr...>>
{
typedef Car car_type;
typedef std::tuple<Cdr...> cdr_type;
};
}
template <class... List>
struct car
{
typedef typename detail::cudder<List...>::car_type type;
};
template <class... List>
struct cdr
{
typedef typename detail::cudder<List...>::cdr_type type;
};
}
#endif
//
// cudder_test.cpp
//
// Compile with C++11/C++0x extensions enabled.
// Example: g++ -std=gnu++0x -o cudder_test cudder_test.cpp
#include "prog_cudder.hpp"
#include <type_traits>
using std::is_same;
using std::nullptr_t;
using prog::car;
using prog::cdr;
int main(int, char**) {
static_assert(is_same<int, car<int, bool, long>::type>::value, "should pass");
static_assert(is_same<bool, car<cdr<int, bool, long>::type>::type>::value, "should pass");
static_assert(is_same<long, car<cdr<cdr<int, bool, long>::type>::type>::type>::value, "should pass");
static_assert(is_same<nullptr_t, cdr<cdr<cdr<int, bool, long>::type>::type>::type>::value, "should pass");
return 0;
}This has been distilled and cleaned up from another thread by the original author.