c++ - Does C++11 require allocators to be default constructible, libstdc++ and libc++ disagree? -
using modified version of howard hinnants's c++11 stack allocator documented here , here, std::basic_string
, compiling gcc
using libstdc++
, following example (see 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 following error(amongst others):
error: no matching function call 'short_alloc<char, 200ul>::short_alloc()' if (__n == 0 && __a == _alloc()) ^
however works without error when compiling clang
, using libc++
(see live).
the stdlibc++
implementation of std::basic_string
expects allocator have default constructor.
does c++11 require allocators default constructible? implementation correct?
no, c++11 not require allocator have default constructor, if @ draft c++11 standard section 17.6.3.5
[allocator.requirements] contains table 28
allocator requirements not contain requirement default constructor , later on in section minimal conforming interface provided:
[ example: following allocator class template supporting minimal interface satisfies requirements of table 28:
template <class tp> struct simpleallocator { typedef tp value_type; simpleallocator(ctor args ); template <class t> simpleallocator(const simpleallocator<t>& other); tp *allocate(std::size_t n); void deallocate(tp *p, std::size_t n); };
—end example ]
which not contain default constructor.
there libstdc++
bug report: basic_string assumes allocators default-constructible says:
the empty-string optimization of basic_string assumes allocators default constructible. while used case in c++98, no longer true in c++11, allocators allowed have state.
consider attached example program. compiling with
g++ -std=c++11 -c t.cpp
produces error message, though should compile fine. problem the "_s_construct" calls "_alloc()", not exist.
note c++11 standard not require default constructors. (section 17.6.3.5, table 28). in particular, simpleallocator example section 17.6.3.5 trigger same bug, too.
and response was:
this hardly c++11 allocator requirement missing std::string, of new requirements missing, , unlikely implemented until switch non-cow string implementation.
this fixed of gcc 5.0
:
fixed gcc 5 (when using new string abi)
we can confirm using gcc 5 on wandbox