File indexing completed on 2025-01-18 09:38:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_ZERO_HPP
0011 #define BOOST_HANA_ZERO_HPP
0012
0013 #include <boost/hana/fwd/zero.hpp>
0014
0015 #include <boost/hana/concept/constant.hpp>
0016 #include <boost/hana/concept/monoid.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/to.hpp>
0019 #include <boost/hana/core/dispatch.hpp>
0020 #include <boost/hana/detail/canonical_constant.hpp>
0021
0022 #include <type_traits>
0023
0024
0025 namespace boost { namespace hana {
0026
0027 template <typename M>
0028 constexpr decltype(auto) zero_t<M>::operator()() const {
0029 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0030 static_assert(hana::Monoid<M>::value,
0031 "hana::zero<M>() requires 'M' to be a Monoid");
0032 #endif
0033
0034 using Zero = BOOST_HANA_DISPATCH_IF(zero_impl<M>,
0035 hana::Monoid<M>::value
0036 );
0037
0038 return Zero::apply();
0039 }
0040
0041
0042 template <typename M, bool condition>
0043 struct zero_impl<M, when<condition>> : default_ {
0044 template <typename ...Args>
0045 static constexpr auto apply(Args&& ...) = delete;
0046 };
0047
0048
0049
0050
0051 template <typename T>
0052 struct zero_impl<T, when<
0053 std::is_arithmetic<T>::value &&
0054 !std::is_same<T, bool>::value
0055 >> {
0056 static constexpr T apply()
0057 { return static_cast<T>(0); }
0058 };
0059
0060
0061
0062
0063 namespace detail {
0064 template <typename C>
0065 struct constant_from_zero {
0066 static constexpr auto value = hana::zero<typename C::value_type>();
0067 using hana_tag = detail::CanonicalConstant<typename C::value_type>;
0068 };
0069 }
0070
0071 template <typename C>
0072 struct zero_impl<C, when<
0073 hana::Constant<C>::value &&
0074 Monoid<typename C::value_type>::value
0075 >> {
0076 static constexpr decltype(auto) apply()
0077 { return hana::to<C>(detail::constant_from_zero<C>{}); }
0078 };
0079 }}
0080
0081 #endif