Given the code:
class Foo {
std::vector<int> items;
std::map<int, int> dictionary;
};
If nothing is ever added to the above vector or map, will either still allocate a block of buffer memory? (In other words, does buffer allocation always happen during container creation or can it be deferred until calls to functions like push_back?)
Is there a standard for handling the timing of initial STL container buffer allocation or is that behavior allowed to vary between STL containers and compilers?
Note: This question is not about the extra bytes such containers would add to the size of class Foo.
(A related subset of this question with an emphasis on allocation size is Initial capacity of vector in C++.)
C++ Reference With C++17 the default constructor is noexcept
iff the allocator construction is noexcept
. So it depends on the used allocator. In VS 2015 the standard constructor is noexcept
.
Clarification: It means that if the allocator is no noexcept
then no block of memory is allocated.
And for your second question: Same reference, its is O(1).