Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!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: Wed, 21 Mar 2012 11:35:35 -0700 (PDT) Organization: http://groups.google.com Lines: 50 Sender: std-cpp-request@vandevoorde.com Approved: stephen.clamage@oracle.com Message-ID: <3008757.1215.1332274822853.JavaMail.geo-discussion-forums@pbjv6> References: NNTP-Posting-Host: iskly4GRrHrimde9VNtwmstZ1Dmsn3HCjYpXWVCra+s= Content-Type: text/plain; charset=ISO-8859-1 X-Trace: news.albasani.net kKsRQuvQ89FwYW1NLntPV1qMjpYfZwmqdQY1PFoEU+3f+cvLfNlv5r2YpvJUh5bPtiIl9tiJHgbQ6MoB7zohrw== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Wed, 21 Mar 2012 18:35:38 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:hKQOlXat8LLgwGAxoqdR6Vv1DU8= X-Original-Date: Tue, 20 Mar 2012 13:20:22 -0700 (PDT) Xref: csiph.com comp.std.c++:453 On Monday, March 19, 2012 11:35:35 PM UTC-7, henrikv wrote: > Hello group! > > When writing parallel code, I often allocate a vector for results. I > allocate the complete vector with a single resize. What I'd like to do > is to tell the resize not to initialize the content, since this will > be overwritten anyway. I'd figure something like: > > struct no_init {} g_no_int; > > struct Dummy > { > Dummy(int a_=0) : a(a_) {} > Dummy(const no_init&) {} // Without a v-table, this shouldn't need > to > touch any part of the created object? > > int a; > }; > > void foo() > { > std::vector dummies; > dummies.resize(1000, g_no_int); > // ... > } > > Proposal: add a method to std::vector that allows resize with a > different constructor than default or copy > constructor: > > template > void resize_construct_using_translation(size_t n, const T2&t2); > > Any thoughts on this? > > Best regards, > Henrik Vallgren > This looks more like an improper use of `std::vector` than anything that should be added to the specification. 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. -- [ 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 ]