Skip to content

Commit a59e722

Browse files
committed
Address review comments
1 parent 5b781cc commit a59e722

7 files changed

Lines changed: 153 additions & 56 deletions

File tree

libcudacxx/include/cuda/std/__complex/tuple.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
_CCCL_BEGIN_NAMESPACE_CUDA_STD
3434

3535
template <class _Tp>
36-
struct tuple_size<complex<_Tp>> : ::cuda::std::integral_constant<size_t, 2>
36+
struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2>
3737
{};
3838

3939
template <size_t _Index, class _Tp>
40-
struct tuple_element<_Index, complex<_Tp>> : ::cuda::std::enable_if < _Index<2, _Tp>
40+
struct tuple_element<_Index, complex<_Tp>> : enable_if<(_Index < 2), _Tp>
4141
{};
4242

4343
template <class _Tp>
@@ -100,16 +100,6 @@ template <size_t _Index, class _Tp>
100100
return __get_complex_impl<_Tp>::template get<_Index>(::cuda::std::move(__z));
101101
}
102102

103-
#if _CCCL_HAS_HOST_STD_LIB()
104-
template <class _Tp>
105-
struct tuple_size<::std::complex<_Tp>> : ::cuda::std::integral_constant<size_t, 2>
106-
{};
107-
108-
template <size_t _Index, class _Tp>
109-
struct tuple_element<_Index, ::std::complex<_Tp>> : ::cuda::std::enable_if < _Index<2, _Tp>
110-
{};
111-
#endif // _CCCL_HAS_HOST_STD_LIB()
112-
113103
_CCCL_END_NAMESPACE_CUDA_STD
114104

115105
#include <cuda/std/__cccl/epilogue.h>

libcudacxx/include/cuda/std/__tuple_dir/tuple_element.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020
# pragma system_header
2121
#endif // no system header
2222

23+
#include <cuda/std/__fwd/array.h>
24+
#include <cuda/std/__fwd/complex.h>
25+
#include <cuda/std/__fwd/pair.h>
2326
#include <cuda/std/__fwd/tuple.h>
2427
#include <cuda/std/__tuple_dir/tuple_indices.h>
2528
#include <cuda/std/__tuple_dir/tuple_types.h>
2629
#include <cuda/std/__type_traits/add_const.h>
2730
#include <cuda/std/__type_traits/add_cv.h>
2831
#include <cuda/std/__type_traits/add_volatile.h>
32+
#include <cuda/std/__type_traits/conditional.h>
33+
#include <cuda/std/__type_traits/enable_if.h>
2934
#include <cuda/std/__type_traits/type_list.h>
3035
#include <cuda/std/cstddef>
3136

@@ -60,7 +65,7 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, const volatile _Tp>
6065
template <size_t _Ip, class... _Types>
6166
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, __tuple_types<_Types...>>
6267
{
63-
static_assert(_Ip < sizeof...(_Types), "tuple_element index out of range");
68+
static_assert(_Ip < sizeof...(_Types), "cuda::std::tuple_element index out of range");
6469
using type _CCCL_NODEBUG_ALIAS = __type_index_c<_Ip, _Types...>;
6570
};
6671

@@ -71,6 +76,19 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, tuple<_Tp...>>
7176
};
7277

7378
#if _CCCL_HAS_HOST_STD_LIB()
79+
template <size_t _Ip, class _Tp, size_t _Np>
80+
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, ::std::array<_Tp, _Np>> : enable_if<(_Ip < _Np), _Tp>
81+
{};
82+
83+
template <size_t _Ip, class _Tp>
84+
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, ::std::complex<_Tp>> : enable_if<(_Ip < 2), _Tp>
85+
{};
86+
87+
template <size_t _Ip, class _T1, class _T2>
88+
struct _CCCL_TYPE_VISIBILITY_DEFAULT
89+
tuple_element<_Ip, ::std::pair<_T1, _T2>> : enable_if<(_Ip < 2), conditional_t<_Ip == 0, _T1, _T2>>
90+
{};
91+
7492
template <size_t _Ip, class... _Tp>
7593
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, ::std::tuple<_Tp...>>
7694
{

libcudacxx/include/cuda/std/__tuple_dir/tuple_size.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
# pragma system_header
2121
#endif // no system header
2222

23+
#include <cuda/std/__fwd/array.h>
24+
#include <cuda/std/__fwd/complex.h>
25+
#include <cuda/std/__fwd/pair.h>
2326
#include <cuda/std/__fwd/tuple.h>
2427
#include <cuda/std/__tuple_dir/tuple_types.h>
2528
#include <cuda/std/__type_traits/enable_if.h>
@@ -64,12 +67,6 @@ template <class... _Tp>
6467
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<tuple<_Tp...>> : public integral_constant<size_t, sizeof...(_Tp)>
6568
{};
6669

67-
#if _CCCL_HAS_HOST_STD_LIB()
68-
template <class... _Tp>
69-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::tuple<_Tp...>> : public integral_constant<size_t, sizeof...(_Tp)>
70-
{};
71-
#endif // _CCCL_HAS_HOST_STD_LIB()
72-
7370
template <class... _Tp>
7471
struct _CCCL_TYPE_VISIBILITY_DEFAULT
7572
tuple_size<__tuple_types<_Tp...>> : public integral_constant<size_t, sizeof...(_Tp)>
@@ -78,6 +75,25 @@ tuple_size<__tuple_types<_Tp...>> : public integral_constant<size_t, sizeof...(_
7875
template <class _Tp>
7976
inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
8077

78+
#if _CCCL_HAS_HOST_STD_LIB()
79+
template <class _Tp, size_t _Np>
80+
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::array<_Tp, _Np>> : public integral_constant<size_t, _Np>
81+
{};
82+
83+
template <class _Tp>
84+
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::complex<_Tp>> : integral_constant<size_t, 2>
85+
{};
86+
87+
template <class _T1, class _T2>
88+
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::pair<_T1, _T2>> : public integral_constant<size_t, 2>
89+
{};
90+
91+
template <class... _Tp>
92+
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::tuple<_Tp...>> : public integral_constant<size_t, sizeof...(_Tp)>
93+
{};
94+
95+
#endif // _CCCL_HAS_HOST_STD_LIB()
96+
8197
_CCCL_END_NAMESPACE_CUDA_STD
8298

8399
#include <cuda/std/__cccl/epilogue.h>

libcudacxx/include/cuda/std/__utility/pair.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -653,30 +653,6 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<1, pair<_T1, _T2>>
653653
using type _CCCL_NODEBUG_ALIAS = _T2;
654654
};
655655

656-
#if _CCCL_HAS_HOST_STD_LIB()
657-
template <class _T1, class _T2>
658-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::pair<_T1, _T2>> : public integral_constant<size_t, 2>
659-
{};
660-
661-
template <size_t _Ip, class _T1, class _T2>
662-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, ::std::pair<_T1, _T2>>
663-
{
664-
static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
665-
};
666-
667-
template <class _T1, class _T2>
668-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<0, ::std::pair<_T1, _T2>>
669-
{
670-
using type _CCCL_NODEBUG_ALIAS = _T1;
671-
};
672-
673-
template <class _T1, class _T2>
674-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<1, ::std::pair<_T1, _T2>>
675-
{
676-
using type _CCCL_NODEBUG_ALIAS = _T2;
677-
};
678-
#endif // _CCCL_HAS_HOST_STD_LIB()
679-
680656
template <size_t _Ip>
681657
struct __get_pair;
682658

libcudacxx/include/cuda/std/array

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,6 @@ struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, array<_Tp, _Size>>
452452
using type = _Tp;
453453
};
454454

455-
#if _CCCL_HAS_HOST_STD_LIB()
456-
template <class _Tp, size_t _Size>
457-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_size<::std::array<_Tp, _Size>> : public integral_constant<size_t, _Size>
458-
{};
459-
460-
template <size_t _Ip, class _Tp, size_t _Size>
461-
struct _CCCL_TYPE_VISIBILITY_DEFAULT tuple_element<_Ip, ::std::array<_Tp, _Size>>
462-
{
463-
static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
464-
using type = _Tp;
465-
};
466-
#endif // _CCCL_HAS_HOST_STD_LIB()
467-
468455
template <size_t _Ip, class _Tp, size_t _Size>
469456
[[nodiscard]] _CCCL_API constexpr _Tp& get(array<_Tp, _Size>& __a) noexcept
470457
{
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the libcu++ Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
// UNSUPPORTED: nvrtc
11+
12+
#include <cuda/std/cassert>
13+
#include <cuda/std/tuple>
14+
15+
#include <complex>
16+
#include <tuple>
17+
#include <utility>
18+
19+
#include "test_macros.h"
20+
21+
template <class STD_TYPE, size_t Index, typename Expected>
22+
__host__ __device__ constexpr void test()
23+
{
24+
static_assert(cuda::std::is_same_v<cuda::std::tuple_element_t<Index, STD_TYPE>, Expected>);
25+
static_assert(cuda::std::is_same_v<cuda::std::tuple_element_t<Index, const STD_TYPE>, const Expected>);
26+
static_assert(cuda::std::is_same_v<cuda::std::tuple_element_t<Index, volatile STD_TYPE>, volatile Expected>);
27+
static_assert(
28+
cuda::std::is_same_v<cuda::std::tuple_element_t<Index, const volatile STD_TYPE>, const volatile Expected>);
29+
}
30+
31+
__host__ __device__ constexpr bool test()
32+
{
33+
test<::std::pair<int, float>, 0, int>();
34+
test<::std::pair<int, float>, 1, float>();
35+
36+
// tuple has the size of the number of template arguments
37+
test<::std::tuple<int>, 0, int>();
38+
test<::std::tuple<int, float>, 0, int>();
39+
test<::std::tuple<int, float>, 1, float>();
40+
test<::std::tuple<int, float, short>, 0, int>();
41+
test<::std::tuple<int, float, short>, 1, float>();
42+
test<::std::tuple<int, float, short>, 2, short>();
43+
44+
// array has always the same element
45+
test<::std::array<int, 4>, 0, int>();
46+
test<::std::array<int, 4>, 1, int>();
47+
test<::std::array<int, 4>, 2, int>();
48+
test<::std::array<int, 4>, 3, int>();
49+
50+
return true;
51+
}
52+
53+
int main(int arg, char** argv)
54+
{
55+
test();
56+
static_assert(test());
57+
return 0;
58+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the libcu++ Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
// UNSUPPORTED: nvrtc
11+
12+
#include <cuda/std/cassert>
13+
#include <cuda/std/tuple>
14+
15+
#include <array>
16+
#include <tuple>
17+
#include <utility>
18+
19+
#include "test_macros.h"
20+
21+
template <class STD_TYPE, size_t Size>
22+
__host__ __device__ constexpr void test()
23+
{
24+
static_assert(cuda::std::tuple_size<STD_TYPE>::value == Size);
25+
static_assert(cuda::std::tuple_size<const STD_TYPE>::value == Size);
26+
static_assert(cuda::std::tuple_size<volatile STD_TYPE>::value == Size);
27+
static_assert(cuda::std::tuple_size<const volatile STD_TYPE>::value == Size);
28+
}
29+
30+
__host__ __device__ constexpr bool test()
31+
{
32+
// pair always has a size of 2
33+
test<::std::pair<int, float>, 2>();
34+
35+
// tuple has the size of the number of template arguments
36+
test<::std::tuple<int>, 1>();
37+
test<::std::tuple<int, int>, 2>();
38+
test<::std::tuple<int, int, int>, 3>();
39+
40+
// array has the size of the number of elements
41+
test<::std::array<int, 4>, 4>();
42+
test<::std::array<int, 1337>, 1337>();
43+
44+
return true;
45+
}
46+
47+
int main(int arg, char** argv)
48+
{
49+
test();
50+
static_assert(test());
51+
return 0;
52+
}

0 commit comments

Comments
 (0)