Can sizeof(std::list<T>) vary for different types of T?

Viewed 2269

Can I assume that for any type T, the type std::list<T> will have a same, constant size? Just to make it clear, I mean the size of the 'main' type itself only, not of the memory allocated by it.

It seems logical to me to assume that the size of T itself should only affect the size of list nodes allocated using the allocator.

However, there are two things that might cause the variancy of sizeof(std::list<T>) I can think of:

  1. A standard C++ library trying to 'optimize' the std::list type by putting some of T instances into the std::list<T> itself. That seems like a bad idea to me, and it probably breaks the 'constant time' requirements put by the standard;
  2. A standard C++ having library specializations of std::list<T> for some types, with the specializations having different sizes.

I can't think of any uses for (1) or (2) but I may be wrong.

What I'm thinking of achieving is using a fixed-size slice allocator for a template class usingstd::list<T> inside.


As a note: this is about variance for different types of T, and not for different allocators. I will have an explicit control of all instantiations, and they all will use the std::allocator.

3 Answers
Related