Browse Source

Non-noexcept move operations fixed (#170).

main
offa 4 years ago
parent
commit
51c39a339c
3 changed files with 66 additions and 1 deletions
  1. +5
    -0
      include/detail/wrapper.h
  2. +46
    -1
      test/CallMocks.h
  3. +15
    -0
      test/UniqueResourceTest.cpp

+ 5
- 0
include/detail/wrapper.h View File

@@ -62,6 +62,11 @@ namespace sr::detail
value = std::move(other.value);
}

void reset(const Wrapper<T>& other) noexcept(std::is_nothrow_assignable_v<T, const T&>)
{
value = other.value;
}

void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
{
value = std::forward<T>(newValue);

+ 46
- 1
test/CallMocks.h View File

@@ -204,5 +204,50 @@ namespace mock
static inline bool throwOnNextCopy{false};
};

}
template<bool noexceptMove>
struct NoexceptResource
{
NoexceptResource() = default;
~NoexceptResource() = default;

NoexceptResource(const NoexceptResource&) = default;

NoexceptResource(NoexceptResource&&) noexcept(noexceptMove)
{
}

NoexceptResource& operator=(const NoexceptResource&) = default;

NoexceptResource& operator=(NoexceptResource&&) noexcept(noexceptMove)
{
return *this;
}
};

template<bool noexceptMove>
struct NoexceptDeleter
{
NoexceptDeleter() = default;
~NoexceptDeleter() = default;

NoexceptDeleter(const NoexceptDeleter&) = default;

NoexceptDeleter(NoexceptDeleter&&) noexcept(noexceptMove)
{
}

NoexceptDeleter& operator=(const NoexceptDeleter&) = default;

NoexceptDeleter& operator=(NoexceptDeleter&&) noexcept(noexceptMove)
{
return *this;
}

template<class Resource>
void operator()(const Resource&) const
{
}
};


}

+ 15
- 0
test/UniqueResourceTest.cpp View File

@@ -222,3 +222,18 @@ TEST_CASE("make unique resource checked releases if invalid", "[UniqueResource]"
[[maybe_unused]] auto guard = sr::make_unique_resource_checked(Handle{-1}, Handle{-1}, deleter);
}

TEST_CASE("not noexcept move", "[UniqueResource]")
{
NoexceptDeleter<false> deleter{};
auto guard = sr::unique_resource{NoexceptResource<false>{}, deleter};
auto temp = std::move(guard);
guard = std::move(temp);
}

TEST_CASE("noexcept move", "[UniqueResource]")
{
NoexceptDeleter<true> deleter{};
auto guard = sr::unique_resource{NoexceptResource<true>{}, deleter};
auto temp = std::move(guard);
guard = std::move(temp);
}

Loading…
Cancel
Save