3 Defines `boost::hana::negate`.
5 @copyright Louis Dionne 2013-2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_NEGATE_HPP
11 #define BOOST_HANA_NEGATE_HPP
13 #include <boost/hana/fwd/negate.hpp>
15 #include <boost/hana/concept/group.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/dispatch.hpp>
18 #include <boost/hana/fwd/minus.hpp>
19 #include <boost/hana/zero.hpp>
21 #include <type_traits>
24 BOOST_HANA_NAMESPACE_BEGIN
27 constexpr decltype(auto) negate_t::operator()(X&& x) const {
28 using G = typename hana::tag_of<X>::type;
29 using Negate = BOOST_HANA_DISPATCH_IF(negate_impl<G>,
33 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
34 static_assert(hana::Group<G>::value,
35 "hana::negate(x) requires 'x' to be in a Group");
38 return Negate::apply(static_cast<X&&>(x));
42 template <typename T, bool condition>
43 struct negate_impl<T, when<condition>> : default_ {
45 static constexpr decltype(auto) apply(X&& x)
46 { return hana::minus(hana::zero<T>(), static_cast<X&&>(x)); }
49 //////////////////////////////////////////////////////////////////////////
50 // Model for arithmetic data types
51 //////////////////////////////////////////////////////////////////////////
53 struct negate_impl<T, when<std::is_arithmetic<T>::value &&
54 !std::is_same<bool, T>::value>> {
56 static constexpr decltype(auto) apply(X&& x)
57 { return -static_cast<X&&>(x); }
59 BOOST_HANA_NAMESPACE_END
61 #endif // !BOOST_HANA_NEGATE_HPP