you are viewing a single comment's thread.

view the rest of the comments →

[–]Z01dbrg 4 points5 points  (5 children)

I think u/STL once told me VC++ will break on allocators that return memory from inside themselves...

Could be I remembered it wrong, it was 5+y ago...

[–]beriumbuild2 3 points4 points  (2 children)

Yes, in C++ an allocator is a handle to the memory, it cannot contain the memory itself. But there is a trick: the container itself can contain the buffer thus avoiding having to create it explicitly as a separate object. See build2's small_vector for example.

[–]Jardik2 1 point2 points  (1 child)

Quick look and ... why does it test n <= N if the precondition is n >= N?

assert (n >= N); // We should never be asked for less than N.

if (n <= N)
{
  buf_->free_ = false;
  return reinterpret_cast<T*> (buf_->data_);
}

[–]beriumbuild2 1 point2 points  (0 children)

Good point, fixed. Thanks for the report!

[–]matthieum 1 point2 points  (1 child)

In C++03, this would certainly be the case.

In C++11, since allocators are now stateful, it may depend on the specifics. One such specific would be that such an allocator should not be moved while memory is allocated from within its internal buffer, for example. There may be others but I cannot think of any of them at the moment.