Using a slightly modified version of Howard Hinnants's C++11 stack allocator which is documented here and here, with std::basic_string
and compiling with gcc
which is using libstdc++
, the following example (see it live):
const unsigned int N = 200;
arena<N> a;
short_alloc<char, N> ac(a) ;
std::basic_string<char,std::char_traits<char>,short_alloc<char, N>> empty(ac);
gives the following error(amongst others):
error: no matching function for call to 'short_alloc<char, 200ul>::short_alloc()'
if (__n == 0 && __a == _Alloc())
^
However it works without error when compiling with clang
and using libc++
(see it live).
The stdlibc++
implementation of std::basic_string
expects the allocator to have a default constructor.
Does C++11 require allocators to be default constructible? Which implementation is correct?
No, C++11 does not require an allocator have default constructor, if we look at the draft C++11 standard section
17.6.3.5
[allocator.requirements] it contains Table28
Allocator requirements which does not contain a requirement for a default constructor and later on in the section a minimal conforming interface is provided:which does not contain a default constructor.
There is a
libstdc++
bug report: basic_string assumes that allocators are default-constructible which says:and the response was:
This is fixed as of
gcc 5.0
:We can confirm this using gcc 5 on wandbox