compile.cexpr.recursive.erb.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. template <typename ...xs>
  5. struct list { };
  6. template <typename T>
  7. struct basic_type { using type = T; };
  8. template <typename T>
  9. constexpr basic_type<T> type{};
  10. template <typename x, typename ...xs>
  11. constexpr auto head(list<x, xs...>)
  12. { return type<x>; }
  13. template <typename x, typename ...xs>
  14. constexpr auto tail(list<x, xs...>)
  15. { return list<xs...>{}; }
  16. template <typename F, typename State, typename X, typename ...Xs>
  17. constexpr auto foldl(F f, State s, list<X, Xs...> xs)
  18. { return foldl(f, f(s, head(xs)), tail(xs)); }
  19. template <typename F, typename State>
  20. constexpr auto foldl(F, State s, list<>)
  21. { return s; }
  22. //////////////////////////////////////////////////////////////////////////////
  23. struct f {
  24. template <typename ...>
  25. struct result { };
  26. template <typename X, typename Y>
  27. constexpr auto operator()(X, Y) const
  28. { return result<X, Y>{}; }
  29. };
  30. template <int> struct x { };
  31. struct state { };
  32. int main() {
  33. constexpr auto xs = list<
  34. <%= (1..input_size).map { |i| "x<#{i}>" }.join(', ') %>
  35. >{};
  36. constexpr auto result = foldl(f{}, state{}, xs);
  37. (void)result;
  38. }