In C ++ 11, a function was introduced emplaceto build an element within a sequence. It complements insertthat either copies or moves elements.
However, from multiple overloads insert, only the insert version of one item,
i.e.
iterator insert( const_iterator p, T const& x);
iterator insert( const_iterator p, T&& x );
It has a version emplace,
template< class... Args >
iterator emplace(const_iterator p, Args&&... x);
Is there any reason rather than allowing the creation of elements nin place with emplace?
While overload, for example,
template< class... Args >
iterator emplace(const_iterator p,size_type n,Args&&... x);
like the corresponding insert
iterator insert(const_iterator p,size_type n,const_reference x);
may conflict with another overload by accepting constructor arguments as tupleor using some special tag, for example in_place_t, which can eliminate them.
emplace_n vector ,
template<class... Args>
iterator emplace_n(const_iterator p,size_type n,Args&&... x)
{
size_type const offset = p - begin();
if(capacity() < size()+n)
{
vector<T> v;
v.reserve(size()+n);
v.assign(make_move_iterator(begin(),make_move_iterator(end());
swap(*this,v);
}
auto const sz = size();
for(auto c = 0; c != n ; ++c)
{
emplace_back(x...); //can do forward only if n == 1
}
rotate(begin()+offset,begin()+sz,end());
return iterator{begin() + offset};
}