Standard containers with an std::allocator have their size_type defined as std::size_t. However, is it possible to have an allocator that allocates objects whose size cannot be represented by a size_t? In other words, can a size_type ever be larger than size_t?
Standard containers with an std::allocator have their size_type defined as std::size_t . However, is
Share
Yes, and this could be useful in some cases.
Suppose you have a program that wishes to access more storage than will fit in virtual memory. By creating an allocator that references memory mapped storage and mapping it as required when indirecting
pointerobjects, you can access arbitrarily large amounts of memory.This remains conformant to 18.2:6 because
size_tis defined as large enough to contain the size of any object, but 17.6.3.5:2 table 28 definessize_typeas containing the size of the largest object in the allocation model, which need not be an actual object in the C++ memory model.Note that the requirements in 17.6.3.5:2 table 28 do not constitute a requirement that the allocation of multiple objects should result in an array; for
allocate(n)the requirement is:and for
deallocatethe assertion is:Note area, not array. Another point is 17.6.3.5:4:
There is no requirement here that
(&*p) + nshould be the same asp + n.It’s perfectly legitimate for a model expressible within another model to contain objects not representable in the outer model; for example, non-standard models in mathematical logic.