std::inserter
before | after |
---|---|
|
|
std::inserter
is most commonly used for copying data from a vector
to something like a set
where std::back_inserter
doesn't work. Search your code base for usages of std::inserter
and you'll discover the "hint" argument is consistently and indiscriminately either the container's begin or end iterator. Because this "hint" is never used to hint anything, we propose an overload of the std::inserter
function template that has a single container parameter and returns a corresponding insert iterator. This change in the standard library would simplify a common operation that is a challenge to both teach and explain.
The proposed single argument version of std::inserter
must choose an iterator to pass on to the std::insert_iterator
it constructs. There are three possibilities. Given a passed container x
, we can
x.begin()
,x.end()
, orThese options are semantically equivalent for containers like std::set
. This choice has more important consequences on order preserving types like std::vector
. It is the opinion of the author that the choice of least surprise is to always use x.end()
.
If the committee deems this is worth pursuing, wording will be provided.
This minor change improves readability by better supporting the most common usage pattern. Minor improvements like this could go a long way towards improving C++'s ability keep simple code simple.