Boost logo

Boost Users :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2005-03-03 09:28:58


"Peter Dimov" <pdimov_at_[hidden]> wrote in message
news:006101c51ff2$f5868e30$6501a8c0_at_pdimov2...
> Johan Nilsson wrote:
>
> [...]
>
> > std::vector<shared_ptr<Foo> > foos(10);
> > std::for_each(foos.begin(), foos.end(),
> > _1 = shared_ptr<Foo>(new Foo));
>
> Lambda way (according to the docs, I haven't tried it):
>
> std::for_each( foos.begin(), foos.end(),
> _1 = bind( constructor< shared_ptr<Foo> >(), new_ptr<Foo>() )
> );
>
> (probably leaks when new throws)
>
> Old school way:
>
> shared_ptr<Foo> createFoo()
> {
> return shared_ptr<Foo>( new Foo );
> }
>
> std::generate( foos.begin(), foos.end(), createFoo );

here is also BOOST_FOREACH way:

BOOST_FOREACH( shared_ptr<Foo>&, elem, foos )
    elem.reset( new Foo );

Gennadiy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net