Fix basic_fields move assignment

Fixes #2517
This commit is contained in:
Mohammad Nejati
2024-06-08 20:36:39 +00:00
committed by Mohammad Nejati
parent e55b559ed3
commit e7f49190ef
3 changed files with 83 additions and 63 deletions

View File

@@ -28,62 +28,13 @@ public:
static constexpr std::size_t max_static_buffer =
sizeof(beast::detail::temporary_buffer);
template<class T>
class test_allocator
{
public:
using value_type = T;
test_allocator() noexcept(false) {}
template<class U, class = typename
std::enable_if<!std::is_same<test_allocator, U>::value>::type>
test_allocator(test_allocator<U> const&) noexcept {}
value_type*
allocate(std::size_t n)
{
return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
}
void
deallocate(value_type* p, std::size_t) noexcept
{
::operator delete(p);
}
template<class U>
friend
bool
operator==(test_allocator<T> const&, test_allocator<U> const&) noexcept
{
return true;
}
template<class U>
friend
bool
operator!=(test_allocator<T> const& x, test_allocator<U> const& y) noexcept
{
return !(x == y);
}
};
using test_fields = basic_fields<test_allocator<char>>;
BOOST_STATIC_ASSERT(is_fields<fields>::value);
BOOST_STATIC_ASSERT(is_fields<test_fields>::value);
// std::allocator is noexcept movable, fields should satisfy
// these constraints as well.
BOOST_STATIC_ASSERT(std::is_nothrow_move_constructible<fields>::value);
BOOST_STATIC_ASSERT(std::is_nothrow_move_assignable<fields>::value);
// Check if basic_fields respects throw-constructibility and
// propagate_on_container_move_assignment of the allocator.
BOOST_STATIC_ASSERT(std::is_nothrow_move_constructible<test_fields>::value);
BOOST_STATIC_ASSERT(!std::is_nothrow_move_assignable<test_fields>::value);
template<class Allocator>
using fa_t = basic_fields<Allocator>;
@@ -1072,6 +1023,72 @@ public:
BOOST_STATIC_ASSERT(( insert_test<string_view, const char(&)[10]>::value));
}
template<class T>
class throwing_allocator
{
public:
using value_type = T;
throwing_allocator() noexcept(false) {}
throwing_allocator(throwing_allocator const&) noexcept(false) {}
throwing_allocator(throwing_allocator&&) noexcept(false) {}
throwing_allocator& operator=(throwing_allocator const&) noexcept(false) { return *this; }
throwing_allocator& operator=(throwing_allocator&&) noexcept(false) { return *this; }
template<class U, class = typename
std::enable_if<!std::is_same<throwing_allocator, U>::value>::type>
throwing_allocator(throwing_allocator<U> const&) noexcept(false) {}
value_type*
allocate(std::size_t n)
{
return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
}
void
deallocate(value_type* p, std::size_t) noexcept
{
::operator delete(p);
}
template<class U>
friend
bool
operator==(throwing_allocator<T> const&, throwing_allocator<U> const&) noexcept
{
return true;
}
template<class U>
friend
bool
operator!=(throwing_allocator<T> const& x, throwing_allocator<U> const& y) noexcept
{
return !(x == y);
}
};
void
testIssue2517()
{
using test_fields = basic_fields<throwing_allocator<char>>;
BOOST_STATIC_ASSERT(is_fields<test_fields>::value);
// Check if basic_fields respects throw-constructibility and
// propagate_on_container_move_assignment of the allocator.
BOOST_STATIC_ASSERT(std::is_nothrow_move_constructible<test_fields>::value);
BOOST_STATIC_ASSERT(!std::is_nothrow_move_assignable<test_fields>::value);
test_fields f1;
f1.insert("1", "1");
test_fields f2;
f2 = std::move(f1);
BEAST_EXPECT(f1.begin() == f1.end());
BEAST_EXPECT(f2["1"] == "1");
}
void
testEmpty()
{
@@ -1103,6 +1120,7 @@ public:
testIssue1828();
boost::ignore_unused(&fields_test::testIssue2085);
testIssue2517();
testEmpty();
}
};