1 // Copyright John Maddock 2007.
2 // Copyright Matt Borland 2021.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 This header defines two traits classes, both in namespace boost::math::tools.
10 is_distribution<D>::value is true iff D has overloaded "cdf" and
11 "quantile" functions, plus member typedefs value_type and policy_type.
12 It's not much of a definitive test frankly,
13 but if it looks like a distribution and quacks like a distribution
14 then it must be a distribution.
16 is_scaled_distribution<D>::value is true iff D is a distribution
17 as defined above, and has member functions "scale" and "location".
21 #ifndef BOOST_STATS_IS_DISTRIBUTION_HPP
22 #define BOOST_STATS_IS_DISTRIBUTION_HPP
28 #include <type_traits>
30 namespace boost{ namespace math{ namespace tools{
34 #define BOOST_MATH_HAS_NAMED_TRAIT(trait, name) \
35 template <typename T> \
40 struct no { char x[2]; }; \
42 template <typename U> \
43 static yes test(typename U::name* = nullptr); \
45 template <typename U> \
46 static no test(...); \
49 static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char)); \
52 BOOST_MATH_HAS_NAMED_TRAIT(has_value_type, value_type)
53 BOOST_MATH_HAS_NAMED_TRAIT(has_policy_type, policy_type)
54 BOOST_MATH_HAS_NAMED_TRAIT(has_backend_type, backend_type)
57 char cdf(const D& ...);
59 char quantile(const D& ...);
65 static constexpr bool value = sizeof(cdf(d, 0.0f)) != 1;
72 static constexpr bool value = sizeof(quantile(d, 0.0f)) != 1;
76 struct is_distribution_imp
78 static constexpr bool value =
79 has_quantile<D>::value
81 && has_value_type<D>::value
82 && has_policy_type<D>::value;
85 template <typename sig, sig val>
89 double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
91 char test_has_location(...);
94 double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
96 char test_has_scale(...);
98 template <typename D, bool b>
99 struct is_scaled_distribution_helper
101 static constexpr bool value = false;
104 template <typename D>
105 struct is_scaled_distribution_helper<D, true>
107 static constexpr bool value =
108 (sizeof(test_has_location<D>(0)) != 1)
110 (sizeof(test_has_scale<D>(0)) != 1);
113 template <typename D>
114 struct is_scaled_distribution_imp
116 static constexpr bool value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value);
119 } // namespace detail
121 template <typename T> struct is_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};
122 template <typename T> struct is_scaled_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_scaled_distribution_imp<T>::value> {};