3 ms·
If the allocator is unaware of your type, how do you initialize the underlying object? That is - how do you invoke the default or non-default constructor of typ
by menaerus 3y ago
If the allocator is unaware of your type, how do you initialize the underlying object? That is - how do you invoke the default or non-default constructor of type T over the memory region you just allocated?
- Joker_vD 3y agoThat's not the allocator's job, is it? It's up to the allocator's user to call one of the several available varieties of placement new on the block of memory allocated.
- menaerus 3y ago> That's not the allocator's job, is it? I don't see that point because this is not a technical argument, is it? > It's up to the allocator's user to call one of the several available varieties of placement new on the block of memory allocated. This would have been a point if that's the control you don't have with the allocator interface. But you do. I imagine that you're not suggesting invoking placement-new oneself all over the place whenever one allocates the backing storage?
- Joker_vD 3y agoIt is a technical argument. Allocator's responsibility is to allocate memory. Constructing objects in that memory is not (and must not be) its responsibility. > This would have been a point if that's the control you don't have with the allocator interface. But you do. Well, std::allocator::construct() has been removed in C++20. You still have std::allocator_traits::construct but it just calls std::construct_at(). > you're not suggesting invoking placement-new oneself all over the place whenever one allocates the backing storage Well, that's what all the standard containers do (vector, list, map, etc). Or you can use std::construct_at() instead but that effectively is a variant of the placement new. Of course, you'd better use std::make_unique()/make_shared() (which normally still devolves to placement new IIRC) instead of manipulating raw pointers and invoking constructors manually.