first = allocator::allocate(n);
size_type i;
try {
for (i = 0; i < n; ++i) {
allocator::construct(first + i, x);
}
}
catch(...) {
for(size_type j = 0; j < i; ++j) {
allocator::destroy(first + j);
}
allocator::deallocate(first, n);
throw;
}
}
}
(你可能会问为什么我们手写了这个循环;std::uninitialized_fill()不是已经做我们所需要的东西?几乎,但不完全相同。我们必须调用allocator的构造成员函数而不是简单的pacement new。也许未来的c++标准将会包含一个接受allocator为参数的uninitialized_fill()函数,从而使得不再需要这个显式循环。
析构函数比较简单,因为我们不需要担心异常安全:t的析构函数被假设为从不抛出异常。
template <class t, class allocator>