Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

52 řádky
1.4 KiB

  1. #pragma once
  2. #include <cpputils/mp/operations/if.fwd.h>
  3. #include <cpputils/mp/misc/when.h>
  4. #include <cpputils/mp/misc/tag_of.h>
  5. #include <cpputils/mp/misc/default.h>
  6. #include <cpputils/mp/operations/eval_if.h>
  7. namespace utl {
  8. namespace mp {
  9. namespace __impl
  10. {
  11. template <typename T>
  12. struct hold {
  13. T value;
  14. constexpr T&& operator()() &&
  15. { return static_cast<T&&>(value); }
  16. };
  17. template <typename L, typename = void>
  18. struct if_impl
  19. : if_impl<L, when<true>>
  20. { };
  21. template <typename L, bool condition>
  22. struct if_impl<L, when<condition>>
  23. : default_
  24. {
  25. template <typename Cond, typename Then, typename Else>
  26. static constexpr auto apply(Cond&& c, Then&& t, Else&& e)
  27. {
  28. return eval_if(
  29. std::forward<Cond>(c),
  30. hold<Then&&> { static_cast<Then&&>(t) },
  31. hold<Else&&> { static_cast<Else&&>(e) }
  32. );
  33. }
  34. };
  35. template <typename Cond, typename Then, typename Else>
  36. constexpr auto if_t::operator()(Cond&& c, Then&& t, Else&& e) const
  37. {
  38. using tag_type = tag_of<Cond>;
  39. using if_type = if_impl<tag_type>;
  40. return if_type::apply(std::forward<Cond>(c), std::forward<Then>(t), std::forward<Else>(e));
  41. }
  42. }
  43. }
  44. }