Simple C++ allocator for POD types
If you're going to roll your own allocator in C++17, you can save yourself a lot of grief by just leaving everything to default. The only things you need to include are value_type, allocate(), deallocate(), and operators == and !=.

In fact, I believe by not using the defaults, you've introduced a nasty little bug. Try doing this: auto v = pod_vector&lt;int&gt;(5, 42);. This should create a vector with five ints, all set to 42. But without construct() properly defined, who knows what will happen?

My advice:


Your allocator should be 4 lines.


the static_assert
value_type
allocate()
deallocate()



(And you need the == and != comparisons too.)

At the very least, either remove construct() or give it a sensible implementation (i.e., placement new). With destroy() it's less of an issue because you're only dealing with PODs.



AFTER EDIT 1: I'd wager the reason you're seeing better code generation for malloc() than new is that new throws std::bad_alloc automatically when it can't allocate.

I suspect if you changed your allocate() function to:

auto allocate(std::size_t num)  {
    auto const p = (T*) malloc(sizeof(T) * num);
    if (!p)
        throw std::bad_alloc{};
    return p;
}


you'd see a very different code gen situation.

As it stands, if you get an allocation failure, you're just returning a null pointer... which will lead to undefined behaviour, because allocate() must return a pointer to allocated memory.