I would like to customize std::vector
behavior to not default-construct the element type (e.g. int
), as it is expensive to do this for a large vector.
Looking at this, the only way I can see to do this is to specialize std::allocator_traits<MyAllocator>::construct
. However, this doesn't seem to be possible, because the specialization must be in the same namespace as the original declaration.
Putting a specialization in namespace std
already doesn't seem right. And it's actually worse than that, because the STL implementation I am using actually puts std::allocator_traits
in namespace std::__u
(and that surely varies across STL implementations), so it seems very wrong to do this.
This is confusing because it seems like std::allocator_traits is designed to allow specialization, but I can't figure out how to actually do it. Is this simply a bad idea? If so, is there some other way to solve the problem (avoiding default construction of elements in STL containers)?