Skip to content

Commit bd63dd1

Browse files
authored
Merge pull request NVIDIA#1457 from jrhemstad/fix-const-pair
Fix tuple_size/tuple_element for cv-qualified types
2 parents 66f22c3 + 7ad274b commit bd63dd1

File tree

4 files changed

+92
-45
lines changed

4 files changed

+92
-45
lines changed

testing/pair.cu

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,42 @@ struct TestPairGet
213213
};
214214
SimpleUnitTest<TestPairGet, BuiltinNumericTypes> TestPairGetInstance;
215215

216+
using PairConstVolatileTypes =
217+
unittest::type_list<thrust::pair<int, float>, thrust::pair<int, float> const,
218+
thrust::pair<int, float> const volatile>;
216219

217-
void TestPairTupleSize(void)
220+
template <typename Pair>
221+
struct TestPairTupleSize
218222
{
219-
int result = thrust::tuple_size< thrust::pair<int,int> >::value;
220-
ASSERT_EQUAL(2, result);
223+
void operator()()
224+
{
225+
ASSERT_EQUAL(2, static_cast<int>(thrust::tuple_size<Pair>::value));
226+
}
221227
};
222-
DECLARE_UNITTEST(TestPairTupleSize);
228+
SimpleUnitTest<TestPairTupleSize, PairConstVolatileTypes> TestPairTupleSizeInstance;
223229

224230

225231
void TestPairTupleElement(void)
226232
{
227-
typedef thrust::tuple_element<0, thrust::pair<int, float> >::type type0;
228-
typedef thrust::tuple_element<1, thrust::pair<int, float> >::type type1;
229-
230-
ASSERT_EQUAL_QUIET(typeid(int), typeid(type0));
231-
ASSERT_EQUAL_QUIET(typeid(float), typeid(type1));
233+
using type0 = thrust::tuple_element<0, thrust::pair<int, float> >::type;
234+
using type1 = thrust::tuple_element<1, thrust::pair<int, float> >::type;
235+
static_assert(std::is_same<int, type0>::value,"");
236+
static_assert(std::is_same<float, type1>::value,"");
237+
238+
using c_type0 = thrust::tuple_element<0, thrust::pair<int, float> const>::type;
239+
using c_type1 = thrust::tuple_element<1, thrust::pair<int, float> const>::type;
240+
static_assert(std::is_same<int const, c_type0>::value,"");
241+
static_assert(std::is_same<float const, c_type1>::value,"");
242+
243+
using v_type0 = thrust::tuple_element<0, thrust::pair<int, float> volatile>::type;
244+
using v_type1 = thrust::tuple_element<1, thrust::pair<int, float> volatile>::type;
245+
static_assert(std::is_same<int volatile, v_type0>::value,"");
246+
static_assert(std::is_same<float volatile, v_type1>::value,"");
247+
248+
using cv_type0 = thrust::tuple_element<0, thrust::pair<int, float> const volatile>::type;
249+
using cv_type1 = thrust::tuple_element<1, thrust::pair<int, float> const volatile>::type;
250+
static_assert(std::is_same<int const volatile, cv_type0>::value,"");
251+
static_assert(std::is_same<float const volatile, cv_type1>::value,"");
232252
};
233253
DECLARE_UNITTEST(TestPairTupleElement);
234254

thrust/detail/pair.inl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <thrust/pair.h>
1818
#include <thrust/detail/swap.h>
19+
#include <thrust/tuple.h>
1920

2021
namespace thrust
2122
{
@@ -140,21 +141,21 @@ template <typename T1, typename T2>
140141

141142
// specializations of tuple_element for pair
142143
template<typename T1, typename T2>
143-
struct tuple_element<0, pair<T1,T2> >
144+
struct tuple_element<0, pair<T1,T2>>
144145
{
145146
typedef T1 type;
146147
}; // end tuple_element
147148

148149
template<typename T1, typename T2>
149-
struct tuple_element<1, pair<T1,T2> >
150+
struct tuple_element<1, pair<T1,T2>>
150151
{
151152
typedef T2 type;
152153
}; // end tuple_element
153154

154155

155156
// specialization of tuple_size for pair
156157
template<typename T1, typename T2>
157-
struct tuple_size< pair<T1,T2 > >
158+
struct tuple_size<pair<T1,T2>>
158159
{
159160
static const unsigned int value = 2;
160161
}; // end tuple_size

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<> >

thrust/tuple.h

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,7 @@ struct null_type;
6262
* \see pair
6363
* \see tuple
6464
*/
65-
template<size_t N, class T>
66-
struct tuple_element
67-
{
68-
private:
69-
typedef typename T::tail_type Next;
70-
71-
public:
72-
/*! The result of this metafunction is returned in \c type.
73-
*/
74-
typedef typename tuple_element<N-1, Next>::type type;
75-
}; // end tuple_element
65+
template <size_t N, class T> struct tuple_element;
7666

7767
/*! This metafunction returns the number of elements
7868
* of a \p tuple type of interest.
@@ -82,13 +72,8 @@ template<size_t N, class T>
8272
* \see pair
8373
* \see tuple
8474
*/
85-
template<class T>
86-
struct tuple_size
87-
{
88-
/*! The result of this metafunction is returned in \c value.
89-
*/
90-
static const int value = 1 + tuple_size<typename T::tail_type>::value;
91-
}; // end tuple_size
75+
template <class T> struct tuple_size;
76+
9277

9378
// get function for non-const cons-lists, returns a reference to the element
9479

0 commit comments

Comments
 (0)