9
3

if.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. @file
  3. Defines `boost::hana::if_`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_IF_HPP
  9. #define BOOST_HANA_IF_HPP
  10. #include <boost/hana/fwd/if.hpp>
  11. #include <boost/hana/concept/logical.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/eval_if.hpp>
  15. BOOST_HANA_NAMESPACE_BEGIN
  16. //! @cond
  17. template <typename Cond, typename Then, typename Else>
  18. constexpr decltype(auto) if_t::operator()(Cond&& cond, Then&& then_, Else&& else_) const {
  19. using Bool = typename hana::tag_of<Cond>::type;
  20. using If = BOOST_HANA_DISPATCH_IF(if_impl<Bool>,
  21. hana::Logical<Bool>::value
  22. );
  23. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  24. static_assert(hana::Logical<Bool>::value,
  25. "hana::if_(cond, then, else) requires 'cond' to be a Logical");
  26. #endif
  27. return If::apply(static_cast<Cond&&>(cond),
  28. static_cast<Then&&>(then_),
  29. static_cast<Else&&>(else_));
  30. }
  31. //! @endcond
  32. namespace detail {
  33. template <typename T>
  34. struct hold {
  35. T value;
  36. constexpr T&& operator()() && { return static_cast<T&&>(value); }
  37. };
  38. }
  39. template <typename L, bool condition>
  40. struct if_impl<L, when<condition>> : default_ {
  41. template <typename C, typename T, typename E>
  42. static constexpr auto apply(C&& c, T&& t, E&& e) {
  43. return hana::eval_if(static_cast<C&&>(c),
  44. detail::hold<T&&>{static_cast<T&&>(t)},
  45. detail::hold<E&&>{static_cast<E&&>(e)}
  46. );
  47. }
  48. };
  49. BOOST_HANA_NAMESPACE_END
  50. #endif // !BOOST_HANA_IF_HPP