Skip to content

Commit 0f93dc8

Browse files
committed
Add specializations for tuple_element/size that work for all cv qualified types.
1 parent 520d39f commit 0f93dc8

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

thrust/detail/tuple.inl

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,38 +50,79 @@ template <
5050
class T9 = null_type>
5151
class tuple;
5252

53-
// forward declaration of tuple_element
54-
template<size_t N, class T> struct tuple_element;
5553

56-
// specializations for tuple_element
57-
template<class T>
58-
struct tuple_element<0,T>
59-
{
60-
typedef typename T::head_type type;
61-
}; // end tuple_element<0,T>
54+
template <size_t N, class T> struct tuple_element;
6255

6356
template<size_t N, class T>
64-
struct tuple_element<N, const T>
57+
struct tuple_element_impl
6558
{
6659
private:
6760
typedef typename T::tail_type Next;
68-
typedef typename tuple_element<N-1, Next>::type unqualified_type;
6961

7062
public:
71-
typedef typename thrust::detail::add_const<unqualified_type>::type type;
72-
}; // end tuple_element<N, const T>
63+
/*! The result of this metafunction is returned in \c type.
64+
*/
65+
typedef typename tuple_element_impl<N-1, Next>::type type;
66+
}; // end tuple_element
7367

7468
template<class T>
75-
struct tuple_element<0,const T>
69+
struct tuple_element_impl<0,T>
70+
{
71+
typedef typename T::head_type type;
72+
};
73+
74+
template <size_t N, class T>
75+
struct tuple_element<N, T const>
7676
{
77-
typedef typename thrust::detail::add_const<typename T::head_type>::type type;
78-
}; // end tuple_element<0,const T>
77+
using type = typename std::add_const<typename tuple_element<N, T>::type>::type;
78+
};
7979

80+
template <size_t N, class T>
81+
struct tuple_element<N, T volatile>
82+
{
83+
using type = typename std::add_volatile<typename tuple_element<N, T>::type>::type;
84+
};
8085

86+
template <size_t N, class T>
87+
struct tuple_element<N, T const volatile>
88+
{
89+
using type = typename std::add_cv<typename tuple_element<N, T>::type>::type;
90+
};
91+
92+
template <size_t N, class T>
93+
struct tuple_element{
94+
using type = typename tuple_element_impl<N,T>::type;
95+
};
8196

8297
// forward declaration of tuple_size
8398
template<class T> struct tuple_size;
8499

100+
template<class T>
101+
struct tuple_size<T const> : public tuple_size<T> {};
102+
103+
template<class T>
104+
struct tuple_size<T volatile> : public tuple_size<T> {};
105+
106+
template<class T>
107+
struct tuple_size<T const volatile> : public tuple_size<T> {};
108+
109+
/*! This metafunction returns the number of elements
110+
* of a \p tuple type of interest.
111+
*
112+
* \tparam T A \c tuple type of interest.
113+
*
114+
* \see pair
115+
* \see tuple
116+
*/
117+
template<class T>
118+
struct tuple_size
119+
{
120+
/*! The result of this metafunction is returned in \c value.
121+
*/
122+
static const int value = 1 + tuple_size<typename T::tail_type>::value;
123+
}; // end tuple_size
124+
125+
85126
// specializations for tuple_size
86127
template<>
87128
struct tuple_size< tuple<> >

0 commit comments

Comments
 (0)