Skip to content

Commit 4746be5

Browse files
committed
Tweaks to avoid incomplete typenames for non-tuples in new traits
1 parent 7e00df2 commit 4746be5

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

stan/math/prim/meta.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@
130130
#include <stan/math/prim/meta/scalar_type.hpp>
131131
#include <stan/math/prim/meta/seq_view.hpp>
132132
#include <stan/math/prim/meta/static_select.hpp>
133-
#include <stan/math/prim/meta/tuple_elt.hpp>
133+
#include <stan/math/prim/meta/tuple_element.hpp>
134+
#include <stan/math/prim/meta/tuple_size.hpp>
134135
#include <stan/math/prim/meta/value_type.hpp>
135136
#include <stan/math/prim/meta/void_t.hpp>
136137
#include <stan/math/prim/meta/StdVectorBuilder.hpp>

stan/math/prim/meta/is_tuple.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ inline constexpr bool is_tuple_v = math::is_tuple_v<T>;
6262
* @tparam N expected size
6363
* @ingroup type_trait
6464
*/
65+
template <typename T, std::size_t N, typename = void>
66+
struct is_tuple_of_size : std::false_type {};
67+
6568
template <typename T, std::size_t N>
66-
struct is_tuple_of_size
67-
: std::bool_constant<
68-
is_tuple_v<T> && std::tuple_size_v<std::decay_t<T>> == N> {};
69+
struct is_tuple_of_size<T, N, std::enable_if_t<is_tuple_v<T>>>
70+
: std::bool_constant<std::tuple_size_v<std::decay_t<T>> == N> {};
6971

7072
template <typename T, std::size_t N>
7173
inline constexpr bool is_tuple_of_size_v = is_tuple_of_size<T, N>::value;
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
#ifndef STAN_MATH_PRIM_META_IS_TUPLE_ELT_HPP
2-
#define STAN_MATH_PRIM_META_IS_TUPLE_ELT_HPP
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>
36

47
#include <type_traits>
58
#include <cstddef>
@@ -20,8 +23,8 @@ struct tuple_element {
2023
};
2124

2225
template <std::size_t N, typename T>
23-
struct tuple_element<
24-
N, T, std::enable_if_t<(N < std::tuple_size_v<std::decay_t<T>>)>> {
26+
struct tuple_element<N, T,
27+
std::enable_if_t<is_tuple_v<T> && (N < tuple_size_v<T>)>> {
2528
using type = std::tuple_element_t<N, std::decay_t<T>>;
2629
};
2730

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<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 = tuple_size<T>::value;
26+
} // namespace stan
27+
28+
#endif

0 commit comments

Comments
 (0)