AFAIR none of the standard algorithms allocate memory unless:
- your user-defined type allocates memory during copy or move, or
- your output iterator allocates memory during assignment of a value - for example
std::back_insert_iterator<>
Correction:
The following algorthms use it:
- stable_partition
- inplace_merge
- stable_sort
It seems that the reality is that libstdc++'s implementation simply tries to allocate a buffer using T::operator new
, and halves the allocation size if the new
call returns null, until the allocation size is zero.
template<typename _Tp>
pair<_Tp*, ptrdiff_t>
__get_temporary_buffer(ptrdiff_t __len, _Tp*)
{
const ptrdiff_t __max = numeric_limits<ptrdiff_t>::max() / sizeof(_Tp);
if (__len > __max)
__len = __max;
while (__len > 0)
{
_Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
nothrow));
if (__tmp != 0)
return pair<_Tp*, ptrdiff_t>(__tmp, __len);
__len /= 2;
}
return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
}
source: https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.0/memory-source.html
It turns out that this function is controversial since Alexander Stepanov, an original author of the STL, wrote this as a placeholder implementation, leaving documentation that it should never go into production.
Needless to say, it did, and has been in every port of the STL since.