On Mar 22, 2014, at 12:02 PM, Alisdair Meredith <
alis...@me.com> wrote:
> My simple example of a potentially throwing allocator is one that logs every operation. If the attempt to write to the log fails, that operation may throw.
>
> I am not suggesting that this is a compelling example, but it does seem like a reasonably simple case that could cause exactly these problems. Do we want to outlaw such allocators?
If the logging operation happens before the construction of the element (and throws), there is no problem. If the logging operations throws after the construction of the element, then the client (the container):
1. Can see an exception coming out of allocator_traits::construct, but does not know whether or not the value_type has been constructed.
2. Therefore the container does not know whether or not to catch the exception, destruct the value_type, and then rethrow. Or perhaps, catch the exception, record that a value_type has been constructed (or not), say by incrementing the end() pointer, and then rethrow.
I.e., for vector::push_back, the time from the end of the value_type being constructed, until the time the vector registers the construction with ++end(), the lifetime of that value_type is not owned.
To make it work, somehow the allocator::construct would have to communicate whether or not it actually constructed a value_type. Or perhaps allocator::construct would have to be responsible for destructing the value_type if it threw an exception after constructing it.
Howard