c++ - Why is an allocator allowed to override the ctors and dtors of its value_type? -


as far know, containers supposed use std::allocator_traits<alloc>::construct , std::allocator_traits<alloc>::destroy either alloc::construct , alloc::destroy respectively or value_type::value_type , value_type::~value_type (the defaults).

why allow allocator override default ctor , dtor of value_type? when useful?

this allows allocator construct objects in custom ways.

an example based on standard library features std::scoped_allocator_adaptor pass allocator objects constructs (as long support construction allocators). useful when want elements of container use same allocator container itself. when container's allocator constructs element passes (or copy of rebound element type) element.

another example if have huge vectors of trivial type double don't want pay cost of zero-initializing vector elements if you're going re-assign them new values anyway. can have custom allocator nothing in construct function, elements left uninitialized when vector resized.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -