end.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file end.cc
  3. /// @brief Check use of special object ra::end
  4. // (c) Daniel Llorens - 2016
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include "ra/ra.hh"
  10. #include "ra/test.hh"
  11. using std::cout, std::endl;
  12. namespace ra {
  13. struct End
  14. {
  15. using value_type = dim_t;
  16. constexpr static rank_t rank_s() { return RANK_BAD; }
  17. constexpr static rank_t rank() { return RANK_BAD; }
  18. constexpr static dim_t size_s() { return DIM_BAD; }
  19. constexpr void adv(rank_t const k, dim_t const d) {}
  20. template <class I> constexpr value_type at(I const & i) const { assert(0); return 0; }
  21. constexpr static dim_t size(int const k) { return DIM_BAD; }
  22. constexpr dim_t stride(int const i) const { assert(0); }
  23. constexpr value_type * flat() const { assert(0); }
  24. };
  25. constexpr End end {};
  26. template <class E>
  27. constexpr decltype(auto) replace_end(dim_t end, E && e)
  28. {
  29. return std::forward<E>(e);
  30. }
  31. constexpr auto replace_end(dim_t end, End && e)
  32. {
  33. return scalar(end);
  34. }
  35. template <class Op, class P ...>
  36. constexpr auto replace_end(dim_t end, Expr<Op, std::tuple<P ...>> && e)
  37. {
  38. return expr(std::forward<decltype(e.op)>(e.op),
  39. replace_end(end, std::forward<P>(std::get<I>(e.t))) ...);
  40. }
  41. } // ra
  42. int main()
  43. {
  44. // End is usable in exprs by virtue of having flat() (TODO will fix this).
  45. ra::Small<int, 3> a {1, 2, 3};
  46. cout << replace_end(10, a + ra::end) << endl;
  47. return 0;
  48. }