Topic: initializer_list use in push_back and the like?


Author: Sean Hunt <rideau3@gmail.com>
Date: Sat, 26 Dec 2009 23:56:51 CST
Raw View
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-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Mon, 28 Dec 2009 12:02:24 CST
Raw View
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?


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





Author: Sean Hunt <rideau3@gmail.com>
Date: Wed, 30 Dec 2009 14:40:29 CST
Raw View
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


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





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Thu, 31 Dec 2009 09:32:25 CST
Raw View
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?



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





Author: Sean Hunt <rideau3@gmail.com>
Date: Sat, 2 Jan 2010 01:26:39 CST
Raw View
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


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