Skip to content

Commit 2b0d390

Browse files
update relocation semantics to match p2786r13
Signed-off-by: guptapratykshh <pratykshgupta9999@gmail.com>
1 parent d58dc08 commit 2b0d390

6 files changed

Lines changed: 114 additions & 2 deletions

File tree

libs/core/type_support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(type_support_headers
2020
hpx/type_support/extra_data.hpp
2121
hpx/type_support/identity.hpp
2222
hpx/type_support/is_relocatable.hpp
23+
hpx/type_support/is_replaceable.hpp
2324
hpx/type_support/is_trivially_relocatable.hpp
2425
hpx/type_support/is_contiguous_iterator.hpp
2526
hpx/type_support/lazy_conditional.hpp
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2025 Isidoros Tsaousis-Seiras
2+
//
3+
// SPDX-License-Identifier: BSL-1.0
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#pragma once
8+
9+
#include <hpx/config.hpp>
10+
11+
#include <type_traits>
12+
13+
namespace hpx::experimental {
14+
15+
HPX_CXX_EXPORT template <typename T>
16+
struct is_replaceable
17+
: std::bool_constant<std::is_object_v<T> &&
18+
std::is_move_constructible_v<T> && std::is_move_assignable_v<T> &&
19+
std::is_destructible_v<T> && !std::is_volatile_v<T>>
20+
{
21+
};
22+
23+
HPX_CXX_EXPORT template <typename T>
24+
inline constexpr bool is_replaceable_v = is_replaceable<T>::value;
25+
26+
} // namespace hpx::experimental

libs/core/type_support/include/hpx/type_support/is_trivially_relocatable.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
namespace hpx::experimental {
1414

15+
#if defined(__cpp_lib_trivially_relocatable)
16+
template <typename T>
17+
struct is_trivially_relocatable : std::is_trivially_relocatable<T>
18+
{
19+
};
20+
#else
21+
1522
// All trivially copyable types are trivially relocatable
1623
// Other types should default to false.
1724
HPX_CXX_CORE_EXPORT template <typename T>
@@ -74,6 +81,8 @@ namespace hpx::experimental {
7481
{
7582
};
7683

84+
#endif
85+
7786
HPX_CXX_CORE_EXPORT template <typename T>
7887
inline constexpr bool is_trivially_relocatable_v =
7988
is_trivially_relocatable<T>::value;

libs/core/type_support/include/hpx/type_support/relocate_at.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include <cstring>
1616
#include <type_traits>
1717

18-
#if defined(HPX_HAVE_P1144_STD_RELOCATE_AT)
18+
#if defined(HPX_HAVE_P1144_STD_RELOCATE_AT) || \
19+
defined(__cpp_lib_trivially_relocatable)
1920
#include <memory>
2021
#endif
2122

@@ -118,10 +119,14 @@ namespace hpx::experimental {
118119
// noexcept if the memmove path is taken or if the move path is noexcept
119120
noexcept(detail::relocate_at_helper(src, dst)))
120121
{
122+
#if defined(__cpp_lib_trivially_relocatable)
123+
return std::relocate_at(src, dst);
124+
#else
121125
static_assert(is_relocatable_v<T>,
122126
"new (dst) T(std::move(*src)) must be well-formed");
123127

124128
return detail::relocate_at_helper(src, dst);
129+
#endif
125130
}
126131

127132
HPX_CXX_CORE_EXPORT template <typename T>

libs/core/type_support/tests/unit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ endforeach()
3232

3333
if(HPX_WITH_COMPILE_ONLY_TESTS)
3434
# add compile time tests
35-
set(compile_tests is_contiguous_iterator is_relocatable
35+
set(compile_tests is_contiguous_iterator is_relocatable is_replaceable
3636
is_trivially_relocatable
3737
)
3838

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) 2025 Isidoros Tsaousis-Seiras
2+
//
3+
// SPDX-License-Identifier: BSL-1.0
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <hpx/type_support/is_replaceable.hpp>
8+
9+
#include <cassert>
10+
#include <memory>
11+
#include <mutex>
12+
#include <type_traits>
13+
14+
using hpx::experimental::is_replaceable_v;
15+
16+
// Integral types are replaceable
17+
static_assert(is_replaceable_v<int>);
18+
// Const types are not assignable
19+
static_assert(!is_replaceable_v<int const>);
20+
21+
// Pointer types are replaceable
22+
static_assert(is_replaceable_v<int*>);
23+
static_assert(is_replaceable_v<int const*>);
24+
25+
// Function pointers are replaceable
26+
static_assert(is_replaceable_v<int (*)()>);
27+
28+
// Arrays are not assignable
29+
static_assert(!is_replaceable_v<int[]>);
30+
static_assert(!is_replaceable_v<int[4]>);
31+
32+
// References are not objects
33+
static_assert(!is_replaceable_v<int&>);
34+
static_assert(!is_replaceable_v<int&&>);
35+
36+
// Void types
37+
static_assert(!is_replaceable_v<void>);
38+
39+
// std::mutex is not move assignable
40+
static_assert(!is_replaceable_v<std::mutex>);
41+
42+
struct not_destructible
43+
{
44+
not_destructible(not_destructible const&);
45+
not_destructible(not_destructible&&);
46+
~not_destructible() = delete;
47+
};
48+
static_assert(!is_replaceable_v<not_destructible>);
49+
50+
struct not_move_assignable
51+
{
52+
not_move_assignable& operator=(not_move_assignable&&) = delete;
53+
};
54+
static_assert(!is_replaceable_v<not_move_assignable>);
55+
56+
struct not_move_constructible
57+
{
58+
not_move_constructible(not_move_constructible&&) = delete;
59+
not_move_constructible& operator=(not_move_constructible&&);
60+
};
61+
static_assert(!is_replaceable_v<not_move_constructible>);
62+
63+
struct move_assignable
64+
{
65+
move_assignable(move_assignable&&);
66+
move_assignable& operator=(move_assignable&&);
67+
~move_assignable();
68+
};
69+
static_assert(is_replaceable_v<move_assignable>);
70+
71+
int main() {}

0 commit comments

Comments
 (0)