On Fri, 1 Dec 2017 22:07:26 +0000
Chris Vine <chris@cvine--nospam--.
freeserve.co.uk> wrote:
> On Fri, 1 Dec 2017 21:53:25 +0000
> Vir Campestris <vir.cam...@invalid.invalid> wrote:
> > On 01/12/2017 06:47, Juha Nieminen wrote:
> > > They are not equivalent.
> > >
> > > foo.push_back({something}) is equivalent to to
> > > foo.push_back(s{something}) (because push_back() takes a parameter
> > > of type s by reference). You are effectively constructing an
> > > object of type s first, and then passing that object to
> > > push_back().
> > >
> > > However, foo.emplace_back(something) will take the parameter
> > > as-is, and try to forward it to the constructor of an object of
> > > type s. In this case {3,4} is of type std::initializer_list, and
> > > s has no constructor that takes such a thing.
> > >
> > > This ought to work (for a similar reason why the push_back()
> > > version does): foo.emplace_back(s{3,4});
> >
> > Right. That makes perfect sense.
> >
> > Or does it?
> >
> > In the case foo.push_back(s{something}) what (default) constructor
> > of s is being called? Isn't {3,4} an initialiser list there too?
>
> The initialisation of the temporary in 'foo.push_back(s{3,4})' and
> 'foo.emplace_back(s{3,4})' have effect as a call to a constructor
> taking two arguments if 's' has no initialiser list constructor.