negate.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. @file
  3. Defines `boost::hana::negate`.
  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_NEGATE_HPP
  9. #define BOOST_HANA_NEGATE_HPP
  10. #include <boost/hana/fwd/negate.hpp>
  11. #include <boost/hana/concept/group.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/fwd/minus.hpp>
  15. #include <boost/hana/zero.hpp>
  16. #include <type_traits>
  17. namespace boost { namespace hana {
  18. //! @cond
  19. template <typename X>
  20. constexpr decltype(auto) negate_t::operator()(X&& x) const {
  21. using G = typename hana::tag_of<X>::type;
  22. using Negate = BOOST_HANA_DISPATCH_IF(negate_impl<G>,
  23. hana::Group<G>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Group<G>::value,
  27. "hana::negate(x) requires 'x' to be in a Group");
  28. #endif
  29. return Negate::apply(static_cast<X&&>(x));
  30. }
  31. //! @endcond
  32. template <typename T, bool condition>
  33. struct negate_impl<T, when<condition>> : default_ {
  34. template <typename X>
  35. static constexpr decltype(auto) apply(X&& x)
  36. { return hana::minus(hana::zero<T>(), static_cast<X&&>(x)); }
  37. };
  38. //////////////////////////////////////////////////////////////////////////
  39. // Model for arithmetic data types
  40. //////////////////////////////////////////////////////////////////////////
  41. template <typename T>
  42. struct negate_impl<T, when<std::is_arithmetic<T>::value &&
  43. !std::is_same<bool, T>::value>> {
  44. template <typename X>
  45. static constexpr decltype(auto) apply(X&& x)
  46. { return -static_cast<X&&>(x); }
  47. };
  48. }} // end namespace boost::hana
  49. #endif // !BOOST_HANA_NEGATE_HPP