Skip to content

Commit 48ff637

Browse files
authored
Backport cuda::std::reference_wrapper C++20 features (#6709)
1 parent 5bd0f5b commit 48ff637

File tree

15 files changed

+30
-60
lines changed

15 files changed

+30
-60
lines changed

libcudacxx/include/cuda/std/__functional/reference_wrapper.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,34 @@ class _CCCL_TYPE_VISIBILITY_DEFAULT reference_wrapper : public __weak_result_typ
4141
using type = _Tp;
4242

4343
private:
44-
type* __f_;
44+
type* __f_{};
4545

46-
static _CCCL_API inline void __fun(_Tp&) noexcept;
46+
static _CCCL_API void __fun(_Tp&) noexcept;
4747
static void __fun(_Tp&&) = delete;
4848

4949
public:
50-
template <class _Up,
51-
class = enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, decltype(__fun(declval<_Up>()))>>
52-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 reference_wrapper(_Up&& __u) noexcept(noexcept(__fun(declval<_Up>())))
50+
template <
51+
class _Up,
52+
class = enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, decltype(__fun(::cuda::std::declval<_Up>()))>>
53+
_CCCL_API constexpr reference_wrapper(_Up&& __u) noexcept(noexcept(__fun(::cuda::std::declval<_Up>())))
5354
{
5455
type& __f = static_cast<_Up&&>(__u);
5556
__f_ = ::cuda::std::addressof(__f);
5657
}
5758

5859
// access
59-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 operator type&() const noexcept
60+
_CCCL_API constexpr operator type&() const noexcept
6061
{
6162
return *__f_;
6263
}
63-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 type& get() const noexcept
64+
[[nodiscard]] _CCCL_API constexpr type& get() const noexcept
6465
{
6566
return *__f_;
6667
}
6768

6869
// invoke
6970
template <class... _ArgTypes>
70-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 invoke_result_t<type&, _ArgTypes...> operator()(_ArgTypes&&... __args) const
71+
_CCCL_API constexpr invoke_result_t<type&, _ArgTypes...> operator()(_ArgTypes&&... __args) const
7172
noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)
7273
{
7374
return ::cuda::std::invoke(get(), ::cuda::std::forward<_ArgTypes>(__args)...);
@@ -78,25 +79,25 @@ template <class _Tp>
7879
_CCCL_HOST_DEVICE reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
7980

8081
template <class _Tp>
81-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) noexcept
82+
[[nodiscard]] _CCCL_API constexpr reference_wrapper<_Tp> ref(_Tp& __t) noexcept
8283
{
8384
return reference_wrapper<_Tp>(__t);
8485
}
8586

8687
template <class _Tp>
87-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t) noexcept
88+
[[nodiscard]] _CCCL_API constexpr reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t) noexcept
8889
{
8990
return __t;
9091
}
9192

9293
template <class _Tp>
93-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 reference_wrapper<const _Tp> cref(const _Tp& __t) noexcept
94+
[[nodiscard]] _CCCL_API constexpr reference_wrapper<const _Tp> cref(const _Tp& __t) noexcept
9495
{
9596
return reference_wrapper<const _Tp>(__t);
9697
}
9798

9899
template <class _Tp>
99-
_CCCL_API inline _CCCL_CONSTEXPR_CXX20 reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t) noexcept
100+
[[nodiscard]] _CCCL_API constexpr reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t) noexcept
100101
{
101102
return __t;
102103
}

libcudacxx/test/libcudacxx/std/utilities/function.objects/func.bind_front/bind_front.pass.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,9 @@ __host__ __device__ constexpr bool test()
200200
assert(h(2, 2, 2) == 9);
201201
}
202202

203-
// Make sure we don't treat cuda::std::reference_wrapper specially.
204-
#if TEST_STD_VER > 2017
205-
# if TEST_COMPILER(NVRTC) // reference_wrapper requires `addressof` which is currently not supported with nvrtc
203+
#if TEST_COMPILER(NVRTC) // reference_wrapper requires `addressof` which is currently not supported with nvrtc
206204
if (!TEST_IS_CONSTANT_EVALUATED())
207-
# endif // TEST_COMPILER(NVRTC)
205+
#endif // TEST_COMPILER(NVRTC)
208206
{
209207
auto add = [](cuda::std::reference_wrapper<int> a, cuda::std::reference_wrapper<int> b) {
210208
return a.get() + b.get();
@@ -213,7 +211,6 @@ __host__ __device__ constexpr bool test()
213211
auto f = cuda::std::bind_front(add, cuda::std::ref(i));
214212
assert(f(cuda::std::ref(j)) == 3);
215213
}
216-
#endif
217214

218215
// Make sure we can call a function that's a pointer to a member function.
219216
{

libcudacxx/test/libcudacxx/std/utilities/function.objects/func.invoke/invoke.pass.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ __host__ __device__ void bullet_one_two_tests()
203203
test_b12<int volatile && (NonCopyable&&) volatile&&, int volatile&&>(cuda::std::move(cl));
204204
test_b12<int const volatile && (NonCopyable&&) const volatile&&, int const volatile&&>(cuda::std::move(cl));
205205
}
206-
#ifndef __cuda_std__
207-
// uncomment when reenabling reference_wrapper
208206
{
209207
TestClass cl_obj(42);
210208
cuda::std::reference_wrapper<TestClass> cl(cl_obj);
@@ -231,7 +229,6 @@ __host__ __device__ void bullet_one_two_tests()
231229
test_b12<int volatile&(NonCopyable&&) volatile&, int volatile&>(cuda::std::move(cl));
232230
test_b12<int const volatile&(NonCopyable&&) const volatile&, int const volatile&>(cuda::std::move(cl));
233231
}
234-
#endif
235232
{
236233
TestClass cl_obj(42);
237234
TestClass* cl = &cl_obj;
@@ -278,8 +275,6 @@ __host__ __device__ void bullet_three_four_tests()
278275
test_b34<int volatile&&>(static_cast<Fn volatile&&>(cl));
279276
test_b34<int const volatile&&>(static_cast<Fn const volatile&&>(cl));
280277
}
281-
#ifndef __cuda_std__
282-
// uncomment when reenabling reference_wrapper
283278
{
284279
typedef TestClass Fn;
285280
Fn cl(42);
@@ -296,7 +291,6 @@ __host__ __device__ void bullet_three_four_tests()
296291
test_b34<int volatile&>(cuda::std::reference_wrapper<Fn volatile>(cl));
297292
test_b34<int const volatile&>(cuda::std::reference_wrapper<Fn const volatile>(cl));
298293
}
299-
#endif
300294
{
301295
typedef TestClass Fn;
302296
Fn cl_obj(42);

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/binder_typedefs.compile.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
// REQUIRES: c++03 || c++11 || c++14 || c++17
10+
// REQUIRES: c++17
1111

1212
// ADDITIONAL_COMPILE_DEFINITIONS: CCCL_IGNORE_DEPRECATED_API
1313

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/refwrap.const/ctor.incomplete.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
// UNSUPPORTED: c++17
11-
1210
// <functional>
1311
//
1412
// reference_wrapper<T>
1513
//
16-
// where T is an incomplete type (since C++20)
14+
// where T is an incomplete type
1715

1816
// #include <cuda/std/functional>
1917
#include <cuda/std/cassert>

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/refwrap.const/type_conv_ctor.pass.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ int main(int, char**)
6666
{
6767
using Ref = cuda::std::reference_wrapper<int>;
6868
static_assert(noexcept(Ref(nothrow_convertible<true>())));
69-
#if !TEST_COMPILER(NVHPC)
7069
static_assert(!noexcept(Ref(nothrow_convertible<false>())));
71-
#endif // !TEST_COMPILER(NVHPC)
7270
}
7371
{
7472
meow(0);

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/refwrap.const/type_conv_ctor2.pass.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ struct A2
4343

4444
__host__ __device__ void implicitly_convert(cuda::std::reference_wrapper<B>) noexcept;
4545

46-
__host__ __device__ TEST_CONSTEXPR_CXX20 bool test()
46+
__host__ __device__ constexpr bool test()
4747
{
4848
{
4949
A1 a{};
50-
#if !TEST_COMPILER(NVHPC)
50+
#if !_CCCL_COMPILER(GCC, <, 8) && !(_CCCL_COMPILER(MSVC, <, 19, 40) && _CCCL_STD_VER < 2020)
5151
static_assert(!noexcept(implicitly_convert(a)));
52-
#endif // TEST_COMPILER(NVHPC)
52+
#endif // !_CCCL_COMPILER(GCC, <, 8) && !(_CCCL_COMPILER(MSVC, <, 19, 40) && _CCCL_STD_VER < 2020)
5353
cuda::std::reference_wrapper<B> b1 = a;
5454
assert(&b1.get() == &a.b_);
55-
#if !TEST_COMPILER(NVHPC)
55+
#if !_CCCL_COMPILER(GCC, <, 8) && !(_CCCL_COMPILER(MSVC, <, 19, 40) && _CCCL_STD_VER < 2020)
5656
static_assert(!noexcept(b1 = a));
57-
#endif // TEST_COMPILER(NVHPC)
57+
#endif // !_CCCL_COMPILER(GCC, <, 8) && !(_CCCL_COMPILER(MSVC, <, 19, 40) && _CCCL_STD_VER < 2020)
5858
b1 = a;
5959
assert(&b1.get() == &a.b_);
6060
}
@@ -73,9 +73,7 @@ __host__ __device__ TEST_CONSTEXPR_CXX20 bool test()
7373
int main(int, char**)
7474
{
7575
test();
76-
#if TEST_STD_VER > 2017 && !TEST_COMPILER(NVRTC)
7776
static_assert(test());
78-
#endif // TEST_STD_VER > 2017 && !TEST_COMPILER(NVRTC)
7977

8078
return 0;
8179
}

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/refwrap.helpers/cref.incomplete.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
// UNSUPPORTED: c++17
11-
1210
// <functional>
1311
//
1412
// reference_wrapper
1513
//
1614
// template <ObjectType T> reference_wrapper<const T> cref(const T& t);
1715
//
18-
// where T is an incomplete type (since C++20)
16+
// where T is an incomplete type
1917

2018
// #include <cuda/std/functional>
2119
#include <cuda/std/cassert>

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/refwrap.helpers/cref_1.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "test_macros.h"
2121

22-
__host__ __device__ TEST_CONSTEXPR_CXX20 bool test()
22+
__host__ __device__ constexpr bool test()
2323
{
2424
int i = 0;
2525
cuda::std::reference_wrapper<const int> r = cuda::std::cref(i);
@@ -30,9 +30,7 @@ __host__ __device__ TEST_CONSTEXPR_CXX20 bool test()
3030
int main(int, char**)
3131
{
3232
test();
33-
#if TEST_STD_VER > 2017 && !TEST_COMPILER(NVRTC)
3433
static_assert(test());
35-
#endif // TEST_STD_VER > 2017 && !TEST_COMPILER(NVRTC)
3634

3735
return 0;
3836
}

libcudacxx/test/libcudacxx/std/utilities/function.objects/refwrap/refwrap.helpers/cref_2.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct A
2626
__host__ __device__ void cref(A) {}
2727
} // namespace adl
2828

29-
__host__ __device__ TEST_CONSTEXPR_CXX20 bool test()
29+
__host__ __device__ constexpr bool test()
3030
{
3131
{
3232
const int i = 0;
@@ -46,9 +46,7 @@ __host__ __device__ TEST_CONSTEXPR_CXX20 bool test()
4646
int main(int, char**)
4747
{
4848
test();
49-
#if TEST_STD_VER > 2017 && !TEST_COMPILER(NVRTC)
5049
static_assert(test());
51-
#endif // TEST_STD_VER > 2017 && !TEST_COMPILER(NVRTC)
5250

5351
return 0;
5452
}

0 commit comments

Comments
 (0)