Skip to content

Commit a099940

Browse files
authored
Merge pull request #3244 from stan-dev/more-metaprograms
Add a few more type traits for use in stanc
2 parents 2f4fe90 + f03c9da commit a099940

6 files changed

Lines changed: 147 additions & 0 deletions

File tree

stan/math/prim/meta.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595
#include <stan/math/prim/meta/is_eigen_matrix.hpp>
9696
#include <stan/math/prim/meta/is_eigen_matrix_base.hpp>
9797
#include <stan/math/prim/meta/is_eigen_sparse_base.hpp>
98+
#include <stan/math/prim/meta/is_floating_point.hpp>
9899
#include <stan/math/prim/meta/is_fvar.hpp>
100+
#include <stan/math/prim/meta/is_integral.hpp>
99101
#include <stan/math/prim/meta/is_kernel_expression.hpp>
100102
#include <stan/math/prim/meta/is_matrix_cl.hpp>
101103
#include <stan/math/prim/meta/is_matrix.hpp>
@@ -128,6 +130,8 @@
128130
#include <stan/math/prim/meta/scalar_type.hpp>
129131
#include <stan/math/prim/meta/seq_view.hpp>
130132
#include <stan/math/prim/meta/static_select.hpp>
133+
#include <stan/math/prim/meta/tuple_element.hpp>
134+
#include <stan/math/prim/meta/tuple_size.hpp>
131135
#include <stan/math/prim/meta/value_type.hpp>
132136
#include <stan/math/prim/meta/void_t.hpp>
133137
#include <stan/math/prim/meta/StdVectorBuilder.hpp>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef STAN_MATH_PRIM_META_IS_FLOATING_POINT_HPP
2+
#define STAN_MATH_PRIM_META_IS_FLOATING_POINT_HPP
3+
4+
#include <type_traits>
5+
6+
namespace stan {
7+
8+
/**
9+
* Checks if decayed type is a floating point type
10+
* @tparam The type to check
11+
* @ingroup type_trait
12+
*/
13+
template <typename T>
14+
using is_floating_point = std::is_floating_point<std::decay_t<T>>;
15+
16+
template <typename T>
17+
inline constexpr bool is_floating_point_v = stan::is_floating_point<T>::value;
18+
19+
template <typename... Types>
20+
inline constexpr bool is_all_floating_point_v
21+
= (stan::is_floating_point_v<Types> && ...);
22+
23+
template <typename... Types>
24+
inline constexpr bool is_any_floating_point_v
25+
= (stan::is_floating_point_v<Types> || ...);
26+
27+
} // namespace stan
28+
29+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef STAN_MATH_PRIM_META_IS_INTEGRAL_HPP
2+
#define STAN_MATH_PRIM_META_IS_INTEGRAL_HPP
3+
4+
#include <type_traits>
5+
6+
namespace stan {
7+
8+
/**
9+
* Checks if decayed type is integral
10+
* @tparam The type to check
11+
* @ingroup type_trait
12+
*/
13+
template <typename T>
14+
using is_integral = std::is_integral<std::decay_t<T>>;
15+
16+
template <typename T>
17+
inline constexpr bool is_integral_v = stan::is_integral<T>::value;
18+
19+
template <typename... Types>
20+
inline constexpr bool is_all_integral_v = (stan::is_integral_v<Types> && ...);
21+
22+
template <typename... Types>
23+
inline constexpr bool is_any_integral_v = (stan::is_integral_v<Types> || ...);
24+
25+
} // namespace stan
26+
27+
#endif

stan/math/prim/meta/is_tuple.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef STAN_MATH_PRIM_META_IS_TUPLE_HPP
22
#define STAN_MATH_PRIM_META_IS_TUPLE_HPP
33

4+
#include <cstddef>
45
#include <stan/math/prim/meta/require_helpers.hpp>
56
#include <tuple>
67
#include <type_traits>
@@ -49,6 +50,28 @@ using require_all_not_tuple_t
4950
/*! @} */
5051
} // namespace math
5152

53+
template <typename T>
54+
using is_tuple = math::is_tuple<T>;
55+
56+
template <typename T>
57+
inline constexpr bool is_tuple_v = math::is_tuple_v<T>;
58+
59+
/**
60+
* Checks both that T is a tuple and that its size is N
61+
* @tparam T type to retrieve the element from
62+
* @tparam N expected size
63+
* @ingroup type_trait
64+
*/
65+
template <typename T, std::size_t N, typename = void>
66+
struct is_tuple_of_size : std::false_type {};
67+
68+
template <typename T, std::size_t N>
69+
struct is_tuple_of_size<T, N, std::enable_if_t<stan::is_tuple_v<T>>>
70+
: std::bool_constant<std::tuple_size_v<std::decay_t<T>> == N> {};
71+
72+
template <typename T, std::size_t N>
73+
inline constexpr bool is_tuple_of_size_v = stan::is_tuple_of_size<T, N>::value;
74+
5275
} // namespace stan
5376

5477
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef STAN_MATH_PRIM_META_TUPLE_ELEMENT_HPP
2+
#define STAN_MATH_PRIM_META_TUPLE_ELEMENT_HPP
3+
4+
#include <stan/math/prim/meta/is_tuple.hpp>
5+
#include <stan/math/prim/meta/tuple_size.hpp>
6+
7+
#include <type_traits>
8+
#include <cstddef>
9+
#include <tuple>
10+
11+
namespace stan {
12+
13+
/**
14+
* Equivalent to std::tuple_element but returns void if N is out of range
15+
* to avoid a static assertion failure in libstdc++.
16+
* @tparam N index of the element to retrieve
17+
* @tparam T type to retrieve the element from
18+
* @ingroup type_trait
19+
*/
20+
template <std::size_t N, typename T, typename = void>
21+
struct tuple_element {
22+
using type = void;
23+
};
24+
25+
template <std::size_t N, typename T>
26+
struct tuple_element<
27+
N, T,
28+
std::enable_if_t<stan::is_tuple_v<T> && (N < stan::tuple_size_v<T>)>> {
29+
using type = std::tuple_element_t<N, std::decay_t<T>>;
30+
};
31+
32+
template <std::size_t N, typename T>
33+
using tuple_element_t = typename stan::tuple_element<N, T>::type;
34+
} // namespace stan
35+
36+
#endif

stan/math/prim/meta/tuple_size.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef STAN_MATH_PRIM_META_TUPLE_SIZE_HPP
2+
#define STAN_MATH_PRIM_META_TUPLE_SIZE_HPP
3+
4+
#include <stan/math/prim/meta/is_tuple.hpp>
5+
#include <type_traits>
6+
#include <cstddef>
7+
#include <tuple>
8+
9+
namespace stan {
10+
11+
/**
12+
* Equivalent to std::tuple_size but returns 0 T is not a tuple
13+
* @tparam T type to get tuple size of
14+
* @ingroup type_trait
15+
*/
16+
template <typename T, typename = void>
17+
struct tuple_size : std::integral_constant<std::size_t, 0> {};
18+
19+
template <typename T>
20+
struct tuple_size<T, std::enable_if_t<stan::is_tuple_v<T>>>
21+
: std::integral_constant<std::size_t, std::tuple_size_v<std::decay_t<T>>> {
22+
};
23+
24+
template <typename T>
25+
constexpr std::size_t tuple_size_v = stan::tuple_size<T>::value;
26+
} // namespace stan
27+
28+
#endif

0 commit comments

Comments
 (0)