Last active
August 29, 2015 14:23
-
-
Save pfultz2/bfba2bfdca7dec26273c to your computer and use it in GitHub Desktop.
Hana Caclulator
This file contains hidden or 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 <iostream> | |
| #include <string> | |
| #include <type_traits> | |
| #include <utility> | |
| #include <memory> | |
| #include <cassert> | |
| using namespace boost::hana; | |
| using namespace boost::hana::literals; | |
| using namespace std::literals; | |
| #define RETURNS(...) -> decltype(__VA_ARGS__) { return __VA_ARGS__; } | |
| template<char C> | |
| struct fun; | |
| template<> | |
| struct fun<'+'> | |
| { | |
| template<class T, class U> | |
| constexpr auto operator()(T&& x, U&& y) RETURNS(x+y); | |
| }; | |
| template<> | |
| struct fun<'-'> | |
| { | |
| template<class T, class U> | |
| constexpr auto operator()(T&& x, U&& y) RETURNS(x-y); | |
| }; | |
| template<char C> | |
| using digit = std::enable_if_t< | |
| (C >= '0' && C <= '9'), | |
| _integral_constant<int, static_cast<int>(C - 48)> | |
| >; | |
| BOOST_HANA_CONSTEXPR_LAMBDA auto evaluate = [](auto x, auto op, auto y) | |
| RETURNS(fun<decltype(op)::value>()(digit<decltype(x)::value>()(), digit<decltype(y)::value>())); | |
| auto try_evaluate = overload_linearly( | |
| evaluate, | |
| always(nothing) | |
| ); | |
| template<char... Cs> | |
| auto try_evaluate_c() RETURNS(try_evaluate(char_<Cs>...)); | |
| int main() { | |
| BOOST_HANA_CONSTEXPR_CHECK(evaluate(char_<'1'>, char_<'+'>, char_<'2'>) == 1 + 2); | |
| BOOST_HANA_CONSTANT_CHECK(try_evaluate_c<'?', '+', '2'>() == nothing); | |
| BOOST_HANA_CONSTANT_CHECK(try_evaluate_c<'1', '?', '2'>() == nothing); | |
| BOOST_HANA_CONSTANT_CHECK(try_evaluate_c<'1', '+', '?'>() == nothing); | |
| BOOST_HANA_CONSTANT_CHECK(try_evaluate_c<'?', '?', '?'>() == nothing); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment