chain.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/chain.hpp>
  6. #include <boost/hana/config.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/optional.hpp>
  9. namespace hana = boost::hana;
  10. BOOST_HANA_CONSTEXPR_LAMBDA auto deref = [](auto x) -> decltype(*x) {
  11. return *x;
  12. };
  13. BOOST_HANA_CONSTEXPR_LAMBDA auto age = [](auto x) -> decltype(x.age) {
  14. return x.age;
  15. };
  16. BOOST_HANA_CONSTEXPR_LAMBDA auto f = [](auto x) {
  17. return hana::chain(hana::sfinae(deref)(x), hana::sfinae(age));
  18. };
  19. struct Person {
  20. unsigned int age;
  21. // ...
  22. };
  23. int main() {
  24. constexpr Person john{30};
  25. // Can't dereference a non-pointer.
  26. BOOST_HANA_CONSTANT_CHECK(f(john) == hana::nothing);
  27. // `int` has no member named `age`.
  28. BOOST_HANA_CONSTANT_CHECK(f(1) == hana::nothing);
  29. // All is good.
  30. BOOST_HANA_CONSTEXPR_CHECK(f(&john) == hana::just(30u));
  31. }