Skip to content

Instantly share code, notes, and snippets.

@merryhime
Created November 17, 2016 19:03
Show Gist options
  • Save merryhime/d8f9459a7e0e355feb298ed0e2050c06 to your computer and use it in GitHub Desktop.
Save merryhime/d8f9459a7e0e355feb298ed0e2050c06 to your computer and use it in GitHub Desktop.
#include <type_traits>
/// Internal implementation of this header.
namespace mp_impl {
template<class... Ls>
struct mp_append_impl;
template<> struct mp_append_impl<> {
using type = mp_list<>;
};
template<template<class...> class L, class... Ts>
struct mp_append_impl<L<Ts...>> {
using type = L<Ts...>;
};
template<template<class...> class L1, class... T1s, template<class...> class L2, class... T2s, class... Ls>
struct mp_append_impl<L1<T1...>, L2<T2...>, Ls...> {
using type = mp_append<L1<T1..., T2...>, Ls...>;
};
template<template<class> class F1, class T>
struct mp_filter_impl;
template<template<class> class F1, template<class...> class L1, class... T1>
struct mp_filter_impl<F1, L1<T1...>> {
using type = mp_append<typename std::conditional<F1<T1>::value, L1<T1>, L1<>>::type...>;
};
template<template<class> class F1, class T>
struct mp_transform_impl;
template<template<class> class F1, template<class...> class L1, class... T1>
struct mp_transform_impl<F1, L1<T1...>> {
using type = L1<F1<T1>...>;
};
} // namespace mp_impl
/// Metavalue representing true or false.
template<bool v> using mp_bool = std::integral_constant<bool, v>;
/// A list of types.
template <typename... Ts>
struct mp_list {};
/**
* Metafunction that concatenates lists together.
* @tparam Ls A variable number of lists to concatenate.
*/
template<class... Ls>
using mp_append = typename mp_append_impl<Ls...>::type;
/**
* Metafunction that filters a list based on a predicate
* @tparam L The list to filter.
* @tparam F The predicate, a metafunction. F<T>::value must be true or false.
*/
template<template<class> class F, class L>
using mp_filter = typename mp_filter_impl<F, L>::type;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment