2
0
mirror of https://github.com/boostorg/hana.git synced 2026-02-03 09:12:14 +00:00
Files
hana/example/monad/bind.cpp
2014-08-25 12:33:49 -04:00

55 lines
1.4 KiB
C++

/*
@copyright Louis Dionne 2014
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/hana/detail/assert.hpp>
#include <boost/hana/detail/constexpr.hpp>
#include <boost/hana/foreign.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/sandbox/detail/is_valid.hpp>
using namespace boost::hana;
// `sfinae(f)(x...)` returns `just(f(x...))` if `f(x...)` is well-formed,
// and `nothing` otherwise.
BOOST_HANA_CONSTEXPR_LAMBDA auto sfinae = [](auto f) {
return [=](auto ...x) {
return eval_if(detail::is_valid(f)(x...),
[=](auto _) { return just(_(f)(x...)); },
[](auto _) { return nothing; }
);
};
};
BOOST_HANA_CONSTEXPR_LAMBDA auto deref = [](auto x) -> decltype(*x) {
return *x;
};
BOOST_HANA_CONSTEXPR_LAMBDA auto age = [](auto x) -> decltype(x.age) {
return x.age;
};
BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x) {
return bind(sfinae(deref)(x), sfinae(age));
};
struct Person {
unsigned int age;
// ...
};
int main() {
Person john{30};
// Can't dereference a non-pointer.
BOOST_HANA_CONSTANT_ASSERT(f(john) == nothing);
// `int` has no member named `age`.
BOOST_HANA_CONSTANT_ASSERT(f(1) == nothing);
// All is good.
BOOST_HANA_CONSTEXPR_ASSERT(f(&john) == just(30u));
}