foldable.cpp 903 B

12345678910111213141516171819202122232425262728293031
  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/define_struct.hpp>
  6. #include <boost/hana/fold_left.hpp>
  7. #include <boost/hana/second.hpp>
  8. namespace hana = boost::hana;
  9. struct Kitten {
  10. BOOST_HANA_DEFINE_STRUCT(Kitten,
  11. (int, extremely_cute),
  12. (int, cute),
  13. (int, not_so_cute)
  14. );
  15. };
  16. int main() {
  17. constexpr Kitten kitten{5, 10, 0};
  18. BOOST_HANA_CONSTEXPR_CHECK(
  19. hana::fold_left(kitten, 0, [](auto total, auto member) {
  20. // first(member) is the name of the member, here
  21. // "extremely_cute", or "cute" or "not_so_cute",
  22. // and second(member) is its value.
  23. return hana::second(member) + total;
  24. }) == (5 + 10 + 0)
  25. );
  26. }