实现 std::vector::p ush_back 强异常安全
Implementing std::vector::push_back strong exception safety
我正在根据 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 问题的主题,解决方案是......麻烦。
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Pytorch torch.cholesky忽略异常