Skip to content

Instantly share code, notes, and snippets.

@ldionne
Last active August 29, 2015 14:23
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 ldionne/32f61a7661d219ca834d to your computer and use it in GitHub Desktop.
Save ldionne/32f61a7661d219ca834d to your computer and use it in GitHub Desktop.
Making Tick's IntegralConstant a Constant for Hana
#include <boost/hana.hpp>
#include <tick/integral_constant.h>
namespace hana = boost::hana;
template <typename T>
struct TickIntegralConstant { using value_type = T; };
namespace boost { namespace hana {
template <typename T, T n>
struct datatype<tick::integral_constant<T, n>> {
using type = TickIntegralConstant<T>;
};
template <typename T>
struct value_impl<TickIntegralConstant<T>> {
template <typename C>
static constexpr auto apply() { return C::value; }
};
template <typename T, typename C>
struct to_impl<TickIntegralConstant<T>, C, when<
models<Constant, C>() &&
std::is_integral<typename C::value_type>{}()
>>
: embedding<is_embedded<typename C::value_type, T>{}>
{
static_assert(std::is_integral<T>{},
"trying to convert a Constant to a tick::integral_constant of a "
"non-integral type; tick::integral_constant may only hold "
"integral types");
template <typename X>
static constexpr auto apply(X const&) {
constexpr T v = hana::value<X>();
return tick::integral_constant<T, v>{};
}
};
}}
static_assert(
hana::plus(tick::integral_constant<int, 3>{},
tick::integral_constant<int, 7>{})
==
tick::integral_constant<int, 10>{}
, "");
int main() { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment