Remove useless allocator copy in map #49
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I never contributed to boost, I'm not sure what kind of test I should write for this? For now, I ran successfully b2 in
boost/libs/container/test
with this patch applied.This patch adds a variant of map constructors to avoid useless extra allocator copy when using initializer list
Many existing constructors have this form:
map(std::initializer_list<value_type> il, const Compare& comp = Compare(), const allocator_type& a = allocator_type())
The issue is that a temporary allocator_type is constructed, and
passed to the base class where it is used to copy-constructed the
rebound allocator.
This temporary allocator_type here is always destroyed at the end
of the map() constructor. For stateful allocators this is not
desirable.
The solution is to adopt what libc++ is doing and have to constructors:
map(std::initializer_list<value_type> il, const Compare& comp = Compare(), const allocator_type& a)
and
map(std::initializer_list<value_type> il, const Compare& comp = Compare())
This way, unless an allocator is provided by the client, no extra temporary
creation/destruction occurs.