searchable.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/accessors.hpp>
  5. #include <boost/hana/all_of.hpp>
  6. #include <boost/hana/assert.hpp>
  7. #include <boost/hana/define_struct.hpp>
  8. #include <boost/hana/equal.hpp>
  9. #include <boost/hana/find.hpp>
  10. #include <boost/hana/optional.hpp>
  11. #include <boost/hana/second.hpp>
  12. #include <boost/hana/string.hpp>
  13. #include <string>
  14. namespace hana = boost::hana;
  15. struct Person {
  16. BOOST_HANA_DEFINE_STRUCT(Person,
  17. (std::string, name),
  18. (unsigned short, age)
  19. );
  20. };
  21. int main() {
  22. Person john{"John", 30};
  23. BOOST_HANA_RUNTIME_CHECK(
  24. hana::find(john, BOOST_HANA_STRING("name")) == hana::just("John")
  25. );
  26. BOOST_HANA_CONSTANT_CHECK(
  27. hana::find(john, BOOST_HANA_STRING("foobar")) == hana::nothing
  28. );
  29. BOOST_HANA_RUNTIME_CHECK(
  30. hana::all_of(hana::accessors<Person>(), [&](auto a) {
  31. return hana::second(a)(john) == hana::second(a)(john);
  32. })
  33. );
  34. // the above is equivalent to:
  35. BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john));
  36. }