accessors.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/assert.hpp>
  6. #include <boost/hana/at.hpp>
  7. #include <boost/hana/define_struct.hpp>
  8. #include <boost/hana/equal.hpp>
  9. #include <boost/hana/first.hpp>
  10. #include <boost/hana/integral_constant.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. constexpr auto accessors = hana::accessors<Person>();
  23. BOOST_HANA_CONSTANT_CHECK(
  24. hana::first(accessors[hana::size_c<0>]) == BOOST_HANA_STRING("name")
  25. );
  26. BOOST_HANA_CONSTANT_CHECK(
  27. hana::first(accessors[hana::size_c<1>]) == BOOST_HANA_STRING("age")
  28. );
  29. constexpr auto get_name = hana::second(accessors[hana::size_c<0>]);
  30. constexpr auto get_age = hana::second(accessors[hana::size_c<1>]);
  31. Person john{"John", 30};
  32. BOOST_HANA_RUNTIME_CHECK(get_name(john) == "John");
  33. BOOST_HANA_RUNTIME_CHECK(get_age(john) == 30);
  34. }