Skip to content

Commit 51c39a3

Browse files
committed
Non-noexcept move operations fixed (#170).
1 parent 639c910 commit 51c39a3

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

include/detail/wrapper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ namespace sr::detail
6262
value = std::move(other.value);
6363
}
6464

65+
void reset(const Wrapper<T>& other) noexcept(std::is_nothrow_assignable_v<T, const T&>)
66+
{
67+
value = other.value;
68+
}
69+
6570
void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
6671
{
6772
value = std::forward<T>(newValue);

test/CallMocks.h

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,50 @@ namespace mock
204204
static inline bool throwOnNextCopy{false};
205205
};
206206

207-
}
207+
template<bool noexceptMove>
208+
struct NoexceptResource
209+
{
210+
NoexceptResource() = default;
211+
~NoexceptResource() = default;
212+
213+
NoexceptResource(const NoexceptResource&) = default;
214+
215+
NoexceptResource(NoexceptResource&&) noexcept(noexceptMove)
216+
{
217+
}
218+
219+
NoexceptResource& operator=(const NoexceptResource&) = default;
220+
221+
NoexceptResource& operator=(NoexceptResource&&) noexcept(noexceptMove)
222+
{
223+
return *this;
224+
}
225+
};
226+
227+
template<bool noexceptMove>
228+
struct NoexceptDeleter
229+
{
230+
NoexceptDeleter() = default;
231+
~NoexceptDeleter() = default;
232+
233+
NoexceptDeleter(const NoexceptDeleter&) = default;
208234

235+
NoexceptDeleter(NoexceptDeleter&&) noexcept(noexceptMove)
236+
{
237+
}
238+
239+
NoexceptDeleter& operator=(const NoexceptDeleter&) = default;
240+
241+
NoexceptDeleter& operator=(NoexceptDeleter&&) noexcept(noexceptMove)
242+
{
243+
return *this;
244+
}
245+
246+
template<class Resource>
247+
void operator()(const Resource&) const
248+
{
249+
}
250+
};
251+
252+
253+
}

test/UniqueResourceTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,18 @@ TEST_CASE("make unique resource checked releases if invalid", "[UniqueResource]"
222222
[[maybe_unused]] auto guard = sr::make_unique_resource_checked(Handle{-1}, Handle{-1}, deleter);
223223
}
224224

225+
TEST_CASE("not noexcept move", "[UniqueResource]")
226+
{
227+
NoexceptDeleter<false> deleter{};
228+
auto guard = sr::unique_resource{NoexceptResource<false>{}, deleter};
229+
auto temp = std::move(guard);
230+
guard = std::move(temp);
231+
}
232+
233+
TEST_CASE("noexcept move", "[UniqueResource]")
234+
{
235+
NoexceptDeleter<true> deleter{};
236+
auto guard = sr::unique_resource{NoexceptResource<true>{}, deleter};
237+
auto temp = std::move(guard);
238+
guard = std::move(temp);
239+
}

0 commit comments

Comments
 (0)