Skip to content

Commit

Permalink
Merge pull request NVIDIA#1457 from jrhemstad/fix-const-pair
Browse files Browse the repository at this point in the history
Fix tuple_size/tuple_element for cv-qualified types
  • Loading branch information
alliepiper authored Jul 8, 2021
2 parents 66f22c3 + 7ad274b commit bd63dd1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 45 deletions.
38 changes: 29 additions & 9 deletions testing/pair.cu
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,42 @@ struct TestPairGet
};
SimpleUnitTest<TestPairGet, BuiltinNumericTypes> TestPairGetInstance;

using PairConstVolatileTypes =
unittest::type_list<thrust::pair<int, float>, thrust::pair<int, float> const,
thrust::pair<int, float> const volatile>;

void TestPairTupleSize(void)
template <typename Pair>
struct TestPairTupleSize
{
int result = thrust::tuple_size< thrust::pair<int,int> >::value;
ASSERT_EQUAL(2, result);
void operator()()
{
ASSERT_EQUAL(2, static_cast<int>(thrust::tuple_size<Pair>::value));
}
};
DECLARE_UNITTEST(TestPairTupleSize);
SimpleUnitTest<TestPairTupleSize, PairConstVolatileTypes> TestPairTupleSizeInstance;


void TestPairTupleElement(void)
{
typedef thrust::tuple_element<0, thrust::pair<int, float> >::type type0;
typedef thrust::tuple_element<1, thrust::pair<int, float> >::type type1;

ASSERT_EQUAL_QUIET(typeid(int), typeid(type0));
ASSERT_EQUAL_QUIET(typeid(float), typeid(type1));
using type0 = thrust::tuple_element<0, thrust::pair<int, float> >::type;
using type1 = thrust::tuple_element<1, thrust::pair<int, float> >::type;
static_assert(std::is_same<int, type0>::value,"");
static_assert(std::is_same<float, type1>::value,"");

using c_type0 = thrust::tuple_element<0, thrust::pair<int, float> const>::type;
using c_type1 = thrust::tuple_element<1, thrust::pair<int, float> const>::type;
static_assert(std::is_same<int const, c_type0>::value,"");
static_assert(std::is_same<float const, c_type1>::value,"");

using v_type0 = thrust::tuple_element<0, thrust::pair<int, float> volatile>::type;
using v_type1 = thrust::tuple_element<1, thrust::pair<int, float> volatile>::type;
static_assert(std::is_same<int volatile, v_type0>::value,"");
static_assert(std::is_same<float volatile, v_type1>::value,"");

using cv_type0 = thrust::tuple_element<0, thrust::pair<int, float> const volatile>::type;
using cv_type1 = thrust::tuple_element<1, thrust::pair<int, float> const volatile>::type;
static_assert(std::is_same<int const volatile, cv_type0>::value,"");
static_assert(std::is_same<float const volatile, cv_type1>::value,"");
};
DECLARE_UNITTEST(TestPairTupleElement);

Expand Down
7 changes: 4 additions & 3 deletions thrust/detail/pair.inl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <thrust/pair.h>
#include <thrust/detail/swap.h>
#include <thrust/tuple.h>

namespace thrust
{
Expand Down Expand Up @@ -140,21 +141,21 @@ template <typename T1, typename T2>

// specializations of tuple_element for pair
template<typename T1, typename T2>
struct tuple_element<0, pair<T1,T2> >
struct tuple_element<0, pair<T1,T2>>
{
typedef T1 type;
}; // end tuple_element

template<typename T1, typename T2>
struct tuple_element<1, pair<T1,T2> >
struct tuple_element<1, pair<T1,T2>>
{
typedef T2 type;
}; // end tuple_element


// specialization of tuple_size for pair
template<typename T1, typename T2>
struct tuple_size< pair<T1,T2 > >
struct tuple_size<pair<T1,T2>>
{
static const unsigned int value = 2;
}; // end tuple_size
Expand Down
71 changes: 56 additions & 15 deletions thrust/detail/tuple.inl
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,79 @@ template <
class T9 = null_type>
class tuple;

// forward declaration of tuple_element
template<size_t N, class T> struct tuple_element;

// specializations for tuple_element
template<class T>
struct tuple_element<0,T>
{
typedef typename T::head_type type;
}; // end tuple_element<0,T>
template <size_t N, class T> struct tuple_element;

template<size_t N, class T>
struct tuple_element<N, const T>
struct tuple_element_impl
{
private:
typedef typename T::tail_type Next;
typedef typename tuple_element<N-1, Next>::type unqualified_type;

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

template<class T>
struct tuple_element<0,const T>
struct tuple_element_impl<0,T>
{
typedef typename T::head_type type;
};

template <size_t N, class T>
struct tuple_element<N, T const>
{
typedef typename thrust::detail::add_const<typename T::head_type>::type type;
}; // end tuple_element<0,const T>
using type = typename std::add_const<typename tuple_element<N, T>::type>::type;
};

template <size_t N, class T>
struct tuple_element<N, T volatile>
{
using type = typename std::add_volatile<typename tuple_element<N, T>::type>::type;
};

template <size_t N, class T>
struct tuple_element<N, T const volatile>
{
using type = typename std::add_cv<typename tuple_element<N, T>::type>::type;
};

template <size_t N, class T>
struct tuple_element{
using type = typename tuple_element_impl<N,T>::type;
};

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

template<class T>
struct tuple_size<T const> : public tuple_size<T> {};

template<class T>
struct tuple_size<T volatile> : public tuple_size<T> {};

template<class T>
struct tuple_size<T const volatile> : public tuple_size<T> {};

/*! This metafunction returns the number of elements
* of a \p tuple type of interest.
*
* \tparam T A \c tuple type of interest.
*
* \see pair
* \see tuple
*/
template<class T>
struct tuple_size
{
/*! The result of this metafunction is returned in \c value.
*/
static const int value = 1 + tuple_size<typename T::tail_type>::value;
}; // end tuple_size


// specializations for tuple_size
template<>
struct tuple_size< tuple<> >
Expand Down
21 changes: 3 additions & 18 deletions thrust/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,7 @@ struct null_type;
* \see pair
* \see tuple
*/
template<size_t N, class T>
struct tuple_element
{
private:
typedef typename T::tail_type Next;

public:
/*! The result of this metafunction is returned in \c type.
*/
typedef typename tuple_element<N-1, Next>::type type;
}; // end tuple_element
template <size_t N, class T> struct tuple_element;

/*! This metafunction returns the number of elements
* of a \p tuple type of interest.
Expand All @@ -82,13 +72,8 @@ template<size_t N, class T>
* \see pair
* \see tuple
*/
template<class T>
struct tuple_size
{
/*! The result of this metafunction is returned in \c value.
*/
static const int value = 1 + tuple_size<typename T::tail_type>::value;
}; // end tuple_size
template <class T> struct tuple_size;


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

Expand Down

0 comments on commit bd63dd1

Please sign in to comment.