not.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. @file
  3. Defines `boost::hana::not_`.
  4. Copyright Louis Dionne 2013-2022
  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_NOT_HPP
  9. #define BOOST_HANA_NOT_HPP
  10. #include <boost/hana/fwd/not.hpp>
  11. #include <boost/hana/concept/constant.hpp>
  12. #include <boost/hana/concept/logical.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/to.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/detail/canonical_constant.hpp>
  17. #include <type_traits>
  18. namespace boost { namespace hana {
  19. //! @cond
  20. template <typename X>
  21. constexpr decltype(auto) not_t::operator()(X&& x) const {
  22. using Bool = typename hana::tag_of<X>::type;
  23. using Not = BOOST_HANA_DISPATCH_IF(hana::not_impl<Bool>,
  24. hana::Logical<Bool>::value
  25. );
  26. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  27. static_assert(hana::Logical<Bool>::value,
  28. "hana::not_(cond) requires 'cond' to be a Logical");
  29. #endif
  30. return Not::apply(static_cast<X&&>(x));
  31. }
  32. //! @endcond
  33. template <typename L, bool condition>
  34. struct not_impl<L, when<condition>> : hana::default_ {
  35. template <typename ...Args>
  36. static constexpr auto apply(Args&& ...) = delete;
  37. };
  38. template <typename L>
  39. struct not_impl<L, hana::when<std::is_arithmetic<L>::value>> {
  40. template <typename Cond>
  41. static constexpr Cond apply(Cond const& cond)
  42. { return static_cast<Cond>(cond ? false : true); }
  43. };
  44. namespace detail {
  45. template <typename C, typename X>
  46. struct constant_from_not {
  47. static constexpr auto value = hana::not_(hana::value<X>());
  48. using hana_tag = detail::CanonicalConstant<typename C::value_type>;
  49. };
  50. }
  51. template <typename C>
  52. struct not_impl<C, hana::when<
  53. hana::Constant<C>::value &&
  54. hana::Logical<typename C::value_type>::value
  55. >> {
  56. template <typename Cond>
  57. static constexpr auto apply(Cond const&)
  58. { return hana::to<C>(detail::constant_from_not<C, Cond>{}); }
  59. };
  60. }} // end namespace boost::hana
  61. #endif // !BOOST_HANA_NOT_HPP