Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initializer_list use in push_back and the like?

3 views
Skip to first unread message

Sean Hunt

unread,
Dec 27, 2009, 12:56:51 AM12/27/09
to
How come standard library functions like push_back do not accept
initializer_list<T>s? In order to do a multiple insertion, one needs
to do cont.insert(cont.end(), {values}), which seems like a waste and
exactly the thing that push_back and friends should solve.

Is this just an oversight?

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Mathias Gaunard

unread,
Dec 28, 2009, 1:02:24 PM12/28/09
to
On Dec 27, 6:56 am, Sean Hunt <ride...@gmail.com> wrote:
> How come standard library functions like push_back do not accept
> initializer_list<T>s? In order to do a multiple insertion, one needs
> to do cont.insert(cont.end(), {values}), which seems like a waste and
> exactly the thing that push_back and friends should solve.
>
> Is this just an oversight?

push_back already does perfect forwarding of any set of arguments to
the type constructor, so there is no need to add an overload is there?

Sean Hunt

unread,
Dec 30, 2009, 3:40:29 PM12/30/09
to
On Dec 28, 11:02 am, Mathias Gaunard <loufo...@gmail.com> wrote:
> push_back already does perfect forwarding of any set of arguments to
> the type constructor, so there is no need to add an overload is there?

You are thinking of emplace_back, but that's not what I'm referring
too:

vector<int> v;
v.insert(v.end(), {1,2,3,4}); // ok
v.push_back({1,2,3,4}); // not ok

Sean

Mathias Gaunard

unread,
Dec 31, 2009, 10:32:25 AM12/31/09
to
On Dec 30, 9:40 pm, Sean Hunt <ride...@gmail.com> wrote:
> On Dec 28, 11:02 am, Mathias Gaunard <loufo...@gmail.com> wrote:
>
> > push_back already does perfect forwarding of any set of arguments to
> > the type constructor, so there is no need to add an overload is there?
>
> You are thinking of emplace_back

I'm talking of the new push_back with placement insert support as
defined in N2345.

I can't find an emplace_back, be it in N2345 or N3000.

Unfortunately, it seems the working paper doesn't have the
enhancements as exposed in N2345. Anyone knows why that is, even
though the status of the paper says integrated into the working paper?

Sean Hunt

unread,
Jan 2, 2010, 2:26:39 AM1/2/10
to
On Dec 31 2009, 8:32 am, Mathias Gaunard <loufo...@gmail.com> wrote:
> On Dec 30, 9:40 pm, Sean Hunt <ride...@gmail.com> wrote:
>
> > On Dec 28, 11:02 am, Mathias Gaunard <loufo...@gmail.com> wrote:
>
> > > push_back already does perfect forwarding of any set of arguments to
> > > the type constructor, so there is no need to add an overload is there?
>
> > You are thinking of emplace_back
>
> I'm talking of the new push_back with placement insert support as
> defined in N2345.
>
> I can't find an emplace_back, be it in N2345 or N3000.

Ah, I see. Those were renamed to emplace_front() and emplace_back(),
which is in Table 95 of n3000. The difference between emplacement and
initializer list insertion is that an emplacement only adds a single
element; an initializer list insertion inserts a number of elements,
and I do not see why the same should not extend to push_back() or
push_front().

Sean

0 new messages