Skip to content

Commit 9c7148b

Browse files
committed
Constrain unique_ptr/function factories and prefer move/copy in construction_helpers
has_try_allocate_v reported true for unique_ptr<T> built from unique_ptr<T> (and function from function) because the factories were unconstrained, so try_construct picked the factory over the move constructor. Constrain the factories and add a same-type shortcut in construction_helpers. Signed-off-by: Jarosław Pelczar <jarek@jpelczar.com>
1 parent b5dd2f8 commit 9c7148b

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎include/reloco/construction_helpers.hpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ std::enable_if_t<has_try_construct_v<T, Args...> || has_try_allocate_v<T, Args..
138138
std::is_constructible_v<T, Args...>,
139139
result<void>>
140140
construction_helpers::try_construct(allocator_ref alloc, T *storage, Args &&...args) noexcept {
141-
if constexpr (has_try_construct_v<T, Args...>) {
141+
// Same-type argument means move/copy; never route it through factory traits.
142+
if constexpr (sizeof...(Args) == 1 && (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Args>>, T> && ...) &&
143+
std::is_constructible_v<T, Args...> && !has_try_construct_v<T, Args...>) {
144+
new (storage) T(std::forward<Args>(args)...);
145+
return {};
146+
} else if constexpr (has_try_construct_v<T, Args...>) {
142147
static_assert(std::is_nothrow_default_constructible_v<T>,
143148
"Two-phase construction requires a noexcept default constructor for the shell.");
144149
new (storage) T();
@@ -197,7 +202,10 @@ std::enable_if_t<has_try_allocate_v<T, Args...> || has_try_create_v<T, Args...>
197202
std::is_constructible_v<T, Args...>,
198203
result<T>>
199204
construction_helpers::try_allocate(allocator_ref alloc, Args &&...args) noexcept {
200-
if constexpr (has_try_allocate_v<T, Args...>) {
205+
if constexpr (sizeof...(Args) == 1 && (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Args>>, T> && ...) &&
206+
std::is_constructible_v<T, Args...> && !has_try_construct_v<T, Args...>) {
207+
return result<T>(T(std::forward<Args>(args)...));
208+
} else if constexpr (has_try_allocate_v<T, Args...>) {
201209
return T::try_allocate(alloc, std::forward<Args>(args)...);
202210
} else if constexpr (has_try_create_v<T, Args...>) {
203211
return T::try_create(std::forward<Args>(args)...);

‎include/reloco/function.hpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class [[nodiscard]] RELOCO_OWNER function<R(Args...)> : private detail::requires
9090
* storage strategy available for its (decayed) type: a bare function
9191
* pointer, the inline SOO buffer, or a single heap allocation.
9292
*/
93-
template <typename F> [[nodiscard]] static result<function> try_allocate(allocator_ref alloc, F &&func) noexcept {
93+
template <typename F, std::enable_if_t<!std::is_same_v<std::decay_t<F>, function>, int> = 0>
94+
[[nodiscard]] static result<function> try_allocate(allocator_ref alloc, F &&func) noexcept {
9495
using decayed_f = std::decay_t<F>;
9596
static_assert(std::is_invocable_r_v<R, decayed_f &, Args...>,
9697
"function<R(Args...)>: F must be invocable as R(Args...)");
@@ -119,7 +120,8 @@ class [[nodiscard]] RELOCO_OWNER function<R(Args...)> : private detail::requires
119120
* @brief `try_allocate` using the process-wide default allocator (see
120121
* `default_allocator()`).
121122
*/
122-
template <typename F> [[nodiscard]] static result<function> try_create(F &&func) noexcept {
123+
template <typename F, std::enable_if_t<!std::is_same_v<std::decay_t<F>, function>, int> = 0>
124+
[[nodiscard]] static result<function> try_create(F &&func) noexcept {
123125
return try_allocate(default_allocator(), std::forward<F>(func));
124126
}
125127

‎include/reloco/unique_ptr.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ template <typename T> class [[nodiscard]] RELOCO_OWNER unique_ptr {
7373
* `reloco::error`, like every fallible reloco operation -- is forwarded
7474
* as-is.
7575
*/
76-
template <typename... Args>
76+
template <typename... Args, typename = std::enable_if_t<is_try_constructible_v<T, Args...>>>
7777
[[nodiscard]] static result<unique_ptr> try_allocate(allocator_ref alloc, Args &&...args) noexcept {
7878
auto block = alloc.allocate(sizeof(T), alignof(T));
7979
if (!block)
@@ -91,7 +91,8 @@ template <typename T> class [[nodiscard]] RELOCO_OWNER unique_ptr {
9191
* @brief Allocates and constructs a `T` using the process-wide default
9292
* allocator (see `default_allocator()`).
9393
*/
94-
template <typename... Args> [[nodiscard]] static result<unique_ptr> try_create(Args &&...args) noexcept {
94+
template <typename... Args, typename = std::enable_if_t<is_try_constructible_v<T, Args...>>>
95+
[[nodiscard]] static result<unique_ptr> try_create(Args &&...args) noexcept {
9596
return try_allocate(default_allocator(), std::forward<Args>(args)...);
9697
}
9798

‎tests/test_function.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <gtest/gtest.h>
66
#include <reloco/allocator.hpp>
7+
#include <reloco/concepts.hpp>
78
#include <reloco/default_allocator.hpp>
89
#include <reloco/function.hpp>
910
#include <reloco/heap_allocator.hpp>
@@ -264,3 +265,6 @@ TEST(FunctionTest, TryCloneFailsOnNonCopyableCapture) {
264265
TEST(FunctionTest, IsNotTriviallyRelocatable) {
265266
static_assert(!reloco::is_trivially_relocatable_v<reloco::function<int(int)>>);
266267
}
268+
269+
static_assert(!reloco::has_try_allocate_v<reloco::function<int()>, reloco::function<int()>>,
270+
"function's factory must not claim to wrap another function");

‎tests/test_unique_ptr.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,17 @@ TEST(UniquePtrTest, UnsafeGetSkipsTheNullCheck) {
166166
EXPECT_EQ(ptr->unsafe_get()->value, 5);
167167
RELOCO_END_UNSAFE_BUFFER_USAGE
168168
}
169+
170+
namespace {
171+
static_assert(!reloco::has_try_allocate_v<reloco::unique_ptr<plain_widget>, reloco::unique_ptr<plain_widget>>,
172+
"unique_ptr::try_allocate must not claim to build T from a unique_ptr<T>");
173+
}
174+
175+
TEST(UniquePtrTest, TryConstructMovesUniquePtrInsteadOfUsingFactory) {
176+
reloco::allocator_ref heap = reloco::allocator<reloco::heap_allocator_tag>::ref();
177+
auto src = reloco::unique_ptr<plain_widget>::try_allocate(heap, 9);
178+
ASSERT_TRUE(src.has_value());
179+
auto moved = reloco::construction_helpers::try_allocate<reloco::unique_ptr<plain_widget>>(heap, std::move(*src));
180+
ASSERT_TRUE(moved.has_value());
181+
EXPECT_EQ((*moved)->value, 9);
182+
}

0 commit comments

Comments
 (0)