From 54a3d15b313c9f980eca79ccccf68d20ee884130 Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Tue, 8 Jun 2021 15:48:28 -0500 Subject: [PATCH 1/8] Change tuple_size/tuple_element pair specializations to use const. --- testing/pair.cu | 11 +++++++++++ thrust/detail/pair.inl | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/testing/pair.cu b/testing/pair.cu index a213265f3..e0a5e71e5 100644 --- a/testing/pair.cu +++ b/testing/pair.cu @@ -218,6 +218,10 @@ void TestPairTupleSize(void) { int result = thrust::tuple_size< thrust::pair >::value; ASSERT_EQUAL(2, result); + + // test const pair + int const_result = thrust::tuple_size< thrust::pair const >::value; + ASSERT_EQUAL(2, const_result); }; DECLARE_UNITTEST(TestPairTupleSize); @@ -229,6 +233,13 @@ void TestPairTupleElement(void) ASSERT_EQUAL_QUIET(typeid(int), typeid(type0)); ASSERT_EQUAL_QUIET(typeid(float), typeid(type1)); + + // test const pair + typedef thrust::tuple_element<0, thrust::pair const>::type const_type0; + typedef thrust::tuple_element<1, thrust::pair const>::type const_type1; + + ASSERT_EQUAL_QUIET(typeid(int const), typeid(const_type0)); + ASSERT_EQUAL_QUIET(typeid(float const), typeid(const_type1)); }; DECLARE_UNITTEST(TestPairTupleElement); diff --git a/thrust/detail/pair.inl b/thrust/detail/pair.inl index 426668b99..98846261b 100644 --- a/thrust/detail/pair.inl +++ b/thrust/detail/pair.inl @@ -140,13 +140,13 @@ template // specializations of tuple_element for pair template - struct tuple_element<0, pair > + struct tuple_element<0, const pair > { typedef T1 type; }; // end tuple_element template - struct tuple_element<1, pair > + struct tuple_element<1, const pair > { typedef T2 type; }; // end tuple_element @@ -154,7 +154,7 @@ template // specialization of tuple_size for pair template - struct tuple_size< pair > + struct tuple_size< const pair > { static const unsigned int value = 2; }; // end tuple_size From 906f5fa97862509f2dd7f8eb5eb27aa17240e2f2 Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Tue, 8 Jun 2021 15:52:07 -0500 Subject: [PATCH 2/8] west const. --- thrust/detail/pair.inl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/thrust/detail/pair.inl b/thrust/detail/pair.inl index 98846261b..97a2c4b93 100644 --- a/thrust/detail/pair.inl +++ b/thrust/detail/pair.inl @@ -140,13 +140,13 @@ template // specializations of tuple_element for pair template - struct tuple_element<0, const pair > + struct tuple_element<0, pair const> { typedef T1 type; }; // end tuple_element template - struct tuple_element<1, const pair > + struct tuple_element<1, pair const > { typedef T2 type; }; // end tuple_element @@ -154,7 +154,7 @@ template // specialization of tuple_size for pair template - struct tuple_size< const pair > + struct tuple_size< pair const > { static const unsigned int value = 2; }; // end tuple_size From c1c879a48b2efef431f745224a4ab8807e2ab31b Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Tue, 8 Jun 2021 15:52:46 -0500 Subject: [PATCH 3/8] Format. --- thrust/detail/pair.inl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/thrust/detail/pair.inl b/thrust/detail/pair.inl index 97a2c4b93..e20e6c9b7 100644 --- a/thrust/detail/pair.inl +++ b/thrust/detail/pair.inl @@ -146,7 +146,7 @@ template }; // end tuple_element template - struct tuple_element<1, pair const > + struct tuple_element<1, pair const> { typedef T2 type; }; // end tuple_element @@ -154,7 +154,7 @@ template // specialization of tuple_size for pair template - struct tuple_size< pair const > + struct tuple_size const> { static const unsigned int value = 2; }; // end tuple_size From 9bf1ab3e3c519bbd34ab123ba9fe0de304101287 Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Wed, 23 Jun 2021 11:07:17 -0500 Subject: [PATCH 4/8] remove const from pair specialization. --- thrust/detail/pair.inl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/thrust/detail/pair.inl b/thrust/detail/pair.inl index e20e6c9b7..72de38404 100644 --- a/thrust/detail/pair.inl +++ b/thrust/detail/pair.inl @@ -140,13 +140,13 @@ template // specializations of tuple_element for pair template - struct tuple_element<0, pair const> + struct tuple_element<0, pair> { typedef T1 type; }; // end tuple_element template - struct tuple_element<1, pair const> + struct tuple_element<1, pair> { typedef T2 type; }; // end tuple_element @@ -154,7 +154,7 @@ template // specialization of tuple_size for pair template - struct tuple_size const> + struct tuple_size> { static const unsigned int value = 2; }; // end tuple_size From 520d39f6a86eab2b1b467dafcfbf38079cecfcb9 Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Wed, 23 Jun 2021 11:07:27 -0500 Subject: [PATCH 5/8] Add tuple.h to pair.inl. --- thrust/detail/pair.inl | 1 + 1 file changed, 1 insertion(+) diff --git a/thrust/detail/pair.inl b/thrust/detail/pair.inl index 72de38404..a61ff75ad 100644 --- a/thrust/detail/pair.inl +++ b/thrust/detail/pair.inl @@ -16,6 +16,7 @@ #include #include +#include namespace thrust { From 0f93dc8e2e42f4728811cc8983fd86d6e63bfd64 Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Wed, 23 Jun 2021 11:07:54 -0500 Subject: [PATCH 6/8] Add specializations for tuple_element/size that work for all cv qualified types. --- thrust/detail/tuple.inl | 71 ++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/thrust/detail/tuple.inl b/thrust/detail/tuple.inl index 729d84e41..447ee3b37 100644 --- a/thrust/detail/tuple.inl +++ b/thrust/detail/tuple.inl @@ -50,38 +50,79 @@ template < class T9 = null_type> class tuple; -// forward declaration of tuple_element -template struct tuple_element; -// specializations for tuple_element -template - struct tuple_element<0,T> -{ - typedef typename T::head_type type; -}; // end tuple_element<0,T> +template struct tuple_element; template - struct tuple_element + struct tuple_element_impl { private: typedef typename T::tail_type Next; - typedef typename tuple_element::type unqualified_type; public: - typedef typename thrust::detail::add_const::type type; -}; // end tuple_element + /*! The result of this metafunction is returned in \c type. + */ + typedef typename tuple_element_impl::type type; +}; // end tuple_element template - struct tuple_element<0,const T> + struct tuple_element_impl<0,T> +{ + typedef typename T::head_type type; +}; + +template + struct tuple_element { - typedef typename thrust::detail::add_const::type type; -}; // end tuple_element<0,const T> + using type = typename std::add_const::type>::type; +}; +template +struct tuple_element +{ + using type = typename std::add_volatile::type>::type; +}; +template + struct tuple_element +{ + using type = typename std::add_cv::type>::type; +}; + +template +struct tuple_element{ + using type = typename tuple_element_impl::type; +}; // forward declaration of tuple_size template struct tuple_size; +template + struct tuple_size : public tuple_size {}; + +template + struct tuple_size : public tuple_size {}; + +template + struct tuple_size : public tuple_size {}; + +/*! 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 + struct tuple_size +{ + /*! The result of this metafunction is returned in \c value. + */ + static const int value = 1 + tuple_size::value; +}; // end tuple_size + + // specializations for tuple_size template<> struct tuple_size< tuple<> > From c3454355ee04494e1ceed21cc54decb35a65bc4e Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Wed, 23 Jun 2021 11:08:09 -0500 Subject: [PATCH 7/8] Only forward decl tuple_element/size in tuple.h. --- thrust/tuple.h | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/thrust/tuple.h b/thrust/tuple.h index 45df2be6e..37f5210ef 100644 --- a/thrust/tuple.h +++ b/thrust/tuple.h @@ -62,17 +62,7 @@ struct null_type; * \see pair * \see tuple */ -template - 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::type type; -}; // end tuple_element +template struct tuple_element; /*! This metafunction returns the number of elements * of a \p tuple type of interest. @@ -82,13 +72,8 @@ template * \see pair * \see tuple */ -template - struct tuple_size -{ - /*! The result of this metafunction is returned in \c value. - */ - static const int value = 1 + tuple_size::value; -}; // end tuple_size +template struct tuple_size; + // get function for non-const cons-lists, returns a reference to the element From 7ad274b96386132b4f9f21b58fc05d038c748053 Mon Sep 17 00:00:00 2001 From: Jake Hemstad Date: Wed, 23 Jun 2021 11:08:34 -0500 Subject: [PATCH 8/8] Add tests of tuple_element/size for cv qualified pairs. --- testing/pair.cu | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/testing/pair.cu b/testing/pair.cu index e0a5e71e5..f5f6e92b5 100644 --- a/testing/pair.cu +++ b/testing/pair.cu @@ -213,33 +213,42 @@ struct TestPairGet }; SimpleUnitTest TestPairGetInstance; +using PairConstVolatileTypes = + unittest::type_list, thrust::pair const, + thrust::pair const volatile>; -void TestPairTupleSize(void) +template +struct TestPairTupleSize { - int result = thrust::tuple_size< thrust::pair >::value; - ASSERT_EQUAL(2, result); - - // test const pair - int const_result = thrust::tuple_size< thrust::pair const >::value; - ASSERT_EQUAL(2, const_result); + void operator()() + { + ASSERT_EQUAL(2, static_cast(thrust::tuple_size::value)); + } }; -DECLARE_UNITTEST(TestPairTupleSize); +SimpleUnitTest TestPairTupleSizeInstance; void TestPairTupleElement(void) { - typedef thrust::tuple_element<0, thrust::pair >::type type0; - typedef thrust::tuple_element<1, thrust::pair >::type type1; - - ASSERT_EQUAL_QUIET(typeid(int), typeid(type0)); - ASSERT_EQUAL_QUIET(typeid(float), typeid(type1)); - - // test const pair - typedef thrust::tuple_element<0, thrust::pair const>::type const_type0; - typedef thrust::tuple_element<1, thrust::pair const>::type const_type1; - - ASSERT_EQUAL_QUIET(typeid(int const), typeid(const_type0)); - ASSERT_EQUAL_QUIET(typeid(float const), typeid(const_type1)); + using type0 = thrust::tuple_element<0, thrust::pair >::type; + using type1 = thrust::tuple_element<1, thrust::pair >::type; + static_assert(std::is_same::value,""); + static_assert(std::is_same::value,""); + + using c_type0 = thrust::tuple_element<0, thrust::pair const>::type; + using c_type1 = thrust::tuple_element<1, thrust::pair const>::type; + static_assert(std::is_same::value,""); + static_assert(std::is_same::value,""); + + using v_type0 = thrust::tuple_element<0, thrust::pair volatile>::type; + using v_type1 = thrust::tuple_element<1, thrust::pair volatile>::type; + static_assert(std::is_same::value,""); + static_assert(std::is_same::value,""); + + using cv_type0 = thrust::tuple_element<0, thrust::pair const volatile>::type; + using cv_type1 = thrust::tuple_element<1, thrust::pair const volatile>::type; + static_assert(std::is_same::value,""); + static_assert(std::is_same::value,""); }; DECLARE_UNITTEST(TestPairTupleElement);