Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Jason McKesson Newsgroups: comp.std.c++ Subject: =?ISO-8859-1?Q?Re=3A_Request_f=F6r_std=3A=3Avector_and_possibly_others=3A_mi?= =?ISO-8859-1?Q?nimal_initalization?= Date: Sat, 24 Mar 2012 17:55:01 -0700 (PDT) Organization: http://groups.google.com Lines: 34 Sender: std-cpp-request@vandevoorde.com Approved: stephen.clamage@oracle.com Message-ID: <23678521.1162.1332554396754.JavaMail.geo-discussion-forums@pbcp5> References: <3008757.1215.1332274822853.JavaMail.geo-discussion-forums@pbjv6> NNTP-Posting-Host: 8t8qI5u/IvYgZiBucNDobfVzlXqB0H5Q0qNtzAAHQ4I= Content-Type: text/plain; charset=ISO-8859-1 X-Trace: news.albasani.net WSTx4k6GMXC6iJgaVwxSUKmdyHYdPLnHLIZCA4Rljl6FmhW2Z5I9Me5NO6l/3oami3NE4XRsSirfdDhZeIkwvw== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Sun, 25 Mar 2012 00:55:18 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:4A3lkLK2ewDwR7M8JfHJ3oDRTmw= X-Original-Date: Fri, 23 Mar 2012 18:59:56 -0700 (PDT) Xref: csiph.com comp.std.c++:461 On Friday, March 23, 2012 5:10:25 PM UTC-7, henrikv wrote: > On 21 mar, 19:35, Jason McKesson wrote: > > This looks more like an improper use of `std::vector` than anything that should be added to the specification. > > IMO, telling the vector which constructor to use isn't "improper use". > I'm trying to achieve "minimal but proper initialization", so a class > containing something like a std::string would require that member to > be properly initialized. I can do the same thing today, all that's > required is a default constructor that doesn't touch anything that's > not required: > > struct NoInitDummy > { > NoInitDummy() {} > > // Make this behave like a Dummy using conversion operators etc > int a; > }; How "minimal" do you want? `std::vector::resize(size_t n)` will value-initialize each element. Why do you need it to be *default* initialized? > > All you need to do is use `std::vector::reserve` to allocate as much space as you plan to use. Then use `emplace_back` to construct each addition to the `vector` in-place. > > Using reserve+emplace_back isn't a good idea. I'm thinking *parallel* > loop here, so neither order, nor race conditions can be predicted. I didn't say to use `emplace_back` as part of the parallel loop. I meant that you would do that to *initialize* the `vector`, using exactly the constructor you want. -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]