Last active
August 29, 2015 14:23
-
-
Save ldionne/32f61a7661d219ca834d to your computer and use it in GitHub Desktop.
Making Tick's IntegralConstant a Constant for Hana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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