实现 std::vector::p ush_back 强异常安全

Implementing std::vector::push_back strong exception safety

本文关键字:back 异常 安全 ush std vector 实现      更新时间:2023-10-16

我正在根据 2018 年后的圣地亚哥草案 (N4791) 实现我自己的向量,并且对实现强大的异常安全性有一些疑问。

下面是一些代码:

template <typename T, typename Allocator>
void Vector<T, Allocator>::push_back(const T& value)
{
if (buffer_capacity == 0)
{
this->Allocate(this->GetSufficientCapacity(1));
}
if (buffer_size < buffer_capacity)
{
this->Construct(value);
return;
}
auto new_buffer = CreateNewBuffer(this->GetSufficientCapacity(
buffer_size + 1), allocator);
this->MoveAll(new_buffer);
try
{
new_buffer.Construct(value);
}
catch (...)
{
this->Rollback(new_buffer, std::end(new_buffer));
throw;
}
this->Commit(std::move(new_buffer));
}
template <typename T, typename Allocator>
void Vector<T, Allocator>::Allocate(size_type new_capacity)
{
elements = std::allocator_traits<Allocator>::allocate(allocator,
new_capacity);
buffer_capacity = new_capacity;
}
template <typename T, typename Allocator> template <typename... Args>
void Vector<T, Allocator>::Construct(Args&&... args)
{
// TODO: std::to_address
std::allocator_traits<Allocator>::construct(allocator,
elements + buffer_size, std::forward<Args>(args)...);
++buffer_size;
}
template <typename T, typename Allocator>
Vector<T, Allocator> Vector<T, Allocator>::CreateNewBuffer(
size_type new_capacity, const Allocator& new_allocator)
{
Vector new_buffer{new_allocator};
new_buffer.Allocate(new_capacity);
return new_buffer;
}
template <typename T, typename Allocator>
void Vector<T, Allocator>::Move(iterator first, iterator last, Vector& buffer)
{
if (std::is_nothrow_move_constructible_v<T> ||
!std::is_copy_constructible_v<T>)
{
std::move(first, last, std::back_inserter(buffer));
}
else
{
std::copy(first, last, std::back_inserter(buffer));
}
}
template <typename T, typename Allocator
void Vector<T, Allocator>::MoveAll(Vector& buffer)
{
Move(std::begin(*this), std::end(*this), buffer);
}
template <typename T, typename Allocator>
void Vector<T, Allocator>::Rollback(Vector& other, iterator last) noexcept
{
if (!std::is_nothrow_move_constructible_v<T> &&
std::is_copy_constructible_v<T>)
{
return;
}
std::move(std::begin(other), last, std::begin(*this));
}
template <typename T, typename Allocator>
void Vector<T, Allocator>::Commit(Vector&& other) noexcept
{
this->Deallocate();
elements = other.elements;
buffer_capacity = other.buffer_capacity;
buffer_size = other.buffer_size;
allocator = other.allocator;
other.elements = nullptr;
other.buffer_capacity = 0;
other.buffer_size = 0;
}

我看到这段代码有 2 个问题。我试图遵循std::move_if_noexcept逻辑,但是如果元素是 nothrow move 可构造的,但allocator_traits::construct在自定义分配器中的一些日志记录代码中抛出异常怎么办?然后我的MoveAll调用将抛出并仅产生基本保证。这是标准的缺陷吗?是否应该对Allocator::construct有更严格的措辞?

还有一个在Rollback.只有当移动的元素是可分配的 nothrow 移动时,它才能真正产生强大的保证。否则,再次,只有基本保证。这是应该的吗?

基于范围的std::move/copy函数无法提供强大的异常保证。如果发生异常,您需要一个迭代器到最后一个成功复制/移动的元素,以便您可以正确撤消操作。您必须手动执行复制/移动(或编写专门的函数来执行此操作)。

至于你问题的细节,该标准并没有真正解决如果construct发出一个异常,而不是从正在构造的对象的构造函数中抛出的异常,会发生什么。该标准的意图(出于我将在下面解释的原因)可能是这种情况永远不会发生。但我还没有在标准中找到任何关于这一点的声明。因此,让我们暂时假设这是可能的。

为了使分配器感知容器能够提供强异常保证,construct至少不能在构造对象抛出。毕竟,您不知道抛出了什么异常,因此您将无法判断对象是否成功构造。这将使实现标准要求的行为变得不可能。因此,让我们假设用户没有做一些使实现无法的事情。

在这种情况下,您可以编写代码,假设construct发出的任何异常都意味着未构造对象。如果尽管construct给出了将调用noexcept构造函数的参数,但仍发出异常,则假定从未调用过该构造函数。然后你相应地编写代码。

在复制的情况下,您只需要删除任何已复制的元素(当然是相反的顺序)。移动案例有点棘手,但仍然非常可行。您必须将每个成功移动的对象移动分配回其原始位置。

问题出在哪里?vector<T>::*_back不要求T是可移动的。它只要求T可移动的:也就是说,您可以使用分配器在未初始化的内存中构造它们。但是您不会将其移动到未初始化的内存中;您需要将其移动到已存在移自T的位置。因此,为了保留此要求,您需要销毁所有成功移动的T,然后将它们 MoveInsert 回原位。

但是由于 MoveInsert 需要使用construct,如前所述,这可能会抛出...哎呀。事实上,这正是为什么vector的重新分配函数不会移动除非类型是可移动的或不可复制的(如果是后一种情况,你不会得到强异常保证)。

因此,对我来说似乎很清楚,标准期望任何分配器的construct方法仅在所选构造函数抛出时才抛出。没有其他方法可以在vector中实现所需的行为。但鉴于没有明确说明这一要求,我想说这是标准的缺陷。这不是一个新的缺陷,因为我查看了C++17标准而不是工作文件。

显然,自 2014 年以来,这一直是 LWG 问题的主题,解决方案是......麻烦。