foldable.cpp 839 B

1234567891011121314151617181920212223242526
  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/equal.hpp>
  6. #include <boost/hana/fold_left.hpp>
  7. #include <boost/hana/integral_constant.hpp>
  8. #include <boost/hana/plus.hpp>
  9. #include <boost/hana/string.hpp>
  10. #include <boost/hana/value.hpp>
  11. namespace hana = boost::hana;
  12. int main() {
  13. auto sum_string = [](auto str) {
  14. return hana::fold_left(str, hana::int_c<0>, [](auto sum, auto c) {
  15. constexpr int i = hana::value(c) - 48; // convert character to decimal
  16. return sum + hana::int_c<i>;
  17. });
  18. };
  19. BOOST_HANA_CONSTANT_CHECK(
  20. sum_string(BOOST_HANA_STRING("1234")) == hana::int_c<1 + 2 + 3 + 4>
  21. );
  22. }