I was trying to use a custom allocator for std::vector<char>
, but I noticed that std::vector
does not need/use any of the member functions from my allocator. How is this possible?
#include <vector>
struct A : private std::allocator<char> {
typedef std::allocator<char> alloc;
using alloc::value_type;
using alloc::pointer;
using alloc::const_pointer;
using alloc::difference_type;
using alloc::size_type;
using alloc::rebind;
// member functions have been removed, since the program compiles without them
};
int main() {
std::vector<char, A> v;
v.resize(4000);
for (auto& c : v)
if (c)
return 1; // never happens in my environment
return 0; // all elements initialized to 0. How is this possible?
}
I was trying the above program with an online C++11 compiler (LiveWorkSpace), providing g++ 4.7.2, 4.8 and 4.6.3.
Basically allocate()
, deallocate()
, construct()
and destroy()
are not defined in my allocator, yet the program compiles and all the elements will be initialized to 0.