Topic: Minor change to vector requested


Author: HenrikVallgren <henrik.vallgren@streamspace.com>
Date: Sat, 6 Dec 2014 01:27:17 -0800 (PST)
Raw View
------=_Part_2580_1179171364.1417858037088
Content-Type: multipart/alternative;
 boundary="----=_Part_2581_1354020232.1417858037089"

------=_Part_2581_1354020232.1417858037089
Content-Type: text/plain; charset=UTF-8

Hi,

Request:

Make vectors support initialization (constructor and/or resize) from
a different type from which its value_type is constructible from. This is
already available for a single value using the vector::emplace_back
method so extending it is easy.

Alternate solution:

I understand that there's a request to be able to supply a lambda that
performs per value initialization. That would work for me, though
this is simpler.

Motivation:

I use vectors with multithreaded computations in the form of

  const vector<A> -> vector<B>

where the size of A and B are equal. Resizing vector<B> may cause
lots of memory writes that sometimes can be avoided. E.g. my well
behaved 3D points which default initializes its doubles to {0,0,0}.

My idea is to create a constructor that does nothing, using a global marker:

struct minimal_construct{};

class Point3d

{

public:

            Point3d(double x_=0, double y_=0, double z_=0) : x(x_), y(y_),
z(z_)      {}

            Point3d(const minimal_construct &)                {}

....


and then use it to initialize the vector:

    vector<Point3d>   pts(1000, minimal_construct());

This will not work for many kinds of classes - any data that requires
proper
initialization (e.g. strings, pointers) would invalidate this pattern.

Best regards,
Henrik

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_2581_1354020232.1417858037089
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><font face=3D"arial, sans-serif">Hi,</font><div><font face=
=3D"arial, sans-serif"><br></font></div><div><font face=3D"arial, sans-seri=
f">Request:</font></div><div><font face=3D"arial, sans-serif"><br></font></=
div><div><font face=3D"arial, sans-serif">Make vectors support initializati=
on (constructor and/or resize) from&nbsp;</font></div><div><font face=3D"ar=
ial, sans-serif">a&nbsp;</font><font face=3D"arial, sans-serif">different t=
ype&nbsp;</font><span style=3D"font-family: arial, sans-serif;">from which =
its&nbsp;</span><span style=3D"font-family: arial, sans-serif;">value_type =
is constructible from.&nbsp;</span><span style=3D"font-family: arial, sans-=
serif;">This is&nbsp;</span></div><div><span style=3D"font-family: arial, s=
ans-serif;">already available for a single&nbsp;</span><span style=3D"font-=
family: arial, sans-serif;">value using the vector::emplace_back&nbsp;</spa=
n></div><div><span style=3D"font-family: arial, sans-serif;">method so exte=
nding it is easy.</span></div><div><font face=3D"arial, sans-serif"><br></f=
ont></div><div><font face=3D"arial, sans-serif">Alternate solution:</font><=
/div><div><font face=3D"arial, sans-serif"><br></font></div><div><font face=
=3D"arial, sans-serif">I understand that there's a request to be able to su=
pply a lambda&nbsp;</font><span style=3D"font-family: arial, sans-serif;">t=
hat&nbsp;</span></div><div><span style=3D"font-family: arial, sans-serif;">=
performs per value initialization. That would work for me, though</span></d=
iv><div><span style=3D"font-family: arial, sans-serif;">this is simpler.</s=
pan></div><div><font face=3D"arial, sans-serif"><br></font></div><div><font=
 face=3D"arial, sans-serif">Motivation:</font></div><div><div><font face=3D=
"arial, sans-serif"><br></font></div><div><font face=3D"arial, sans-serif">=
I use vectors with multithreaded computations in the form of</font></div><d=
iv><font face=3D"arial, sans-serif"><br></font></div><div><font face=3D"ari=
al, sans-serif">&nbsp; const vector&lt;A&gt; -&gt; vector&lt;B&gt;</font></=
div><div><font face=3D"arial, sans-serif"><br></font></div><div><font face=
=3D"arial, sans-serif">where the size of A and B are equal. Resizing vector=
&lt;B&gt;&nbsp;</font><span style=3D"font-family: arial, sans-serif;">may c=
ause&nbsp;</span></div><div><span style=3D"font-family: arial, sans-serif;"=
>lots of memory writes that sometimes can be avoided. E.g. my well&nbsp;</s=
pan></div><div><font face=3D"arial, sans-serif">behaved 3D points&nbsp;</fo=
nt><span style=3D"font-family: arial, sans-serif;">which default initialize=
s its doubles to {0,0,0}.</span></div><div><font face=3D"arial, sans-serif"=
><br></font></div><div><font face=3D"arial, sans-serif">My idea is to creat=
e a constructor that does nothing, using a global marker:</font></div><div>=
<font face=3D"arial, sans-serif"><br></font></div><div><font size=3D"2" fac=
e=3D"arial, sans-serif"><span style=3D"color: blue;">struct</span><span sty=
le=3D"color: rgb(0, 0, 0);">&nbsp;minimal_construct{};</span><br></font></d=
iv><div><font size=3D"2" face=3D"arial, sans-serif"><br></font></div><div><=
p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0.0001pt; color: rgb(0, 0, 0=
); line-height: normal;"><font size=3D"2" face=3D"arial, sans-serif"><span =
lang=3D"EN-US" style=3D"color: blue;">class</span><span lang=3D"EN-US">&nbs=
p;Point3d<o:p></o:p></span></font></p><p class=3D"MsoNormal" style=3D"margi=
n: 0cm 0cm 0.0001pt; color: rgb(0, 0, 0); line-height: normal;"><span lang=
=3D"EN-US"><font size=3D"2" face=3D"arial, sans-serif">{<o:p></o:p></font><=
/span></p><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0.0001pt; color: =
rgb(0, 0, 0); line-height: normal;"><font size=3D"2" face=3D"arial, sans-se=
rif"><span lang=3D"EN-US" style=3D"color: blue;">public</span><span lang=3D=
"EN-US">:<o:p></o:p></span></font></p><p class=3D"MsoNormal" style=3D"margi=
n: 0cm 0cm 0.0001pt; color: rgb(0, 0, 0); line-height: normal;"><span lang=
=3D"EN-US"><font size=3D"2" face=3D"arial, sans-serif">&nbsp; &nbsp; &nbsp;=
 &nbsp; &nbsp; &nbsp; Point3d(<span style=3D"color: blue;">double</span>&nb=
sp;x_=3D0,&nbsp;<span style=3D"color: blue;">double</span>&nbsp;y_=3D0,&nbs=
p;<span style=3D"color: blue;">double</span>&nbsp;z_=3D0) : x(x_), y(y_), z=
(z_)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {}<o:p></o:p></font></span></p><p class=
=3D"MsoNormal" style=3D"margin: 0cm 0cm 0.0001pt; color: rgb(0, 0, 0); line=
-height: normal;"><span lang=3D"EN-US"><font size=3D"2" face=3D"arial, sans=
-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point3d(<span style=3D"co=
lor: blue;">const</span>&nbsp;minimal_construct &amp;)&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {}<o:=
p></o:p></font></span></p><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0=
..0001pt; color: rgb(0, 0, 0); line-height: normal;"><font size=3D"2" face=
=3D"arial, sans-serif">...</font></p><p class=3D"MsoNormal" style=3D"margin=
: 0cm 0cm 0.0001pt; color: rgb(0, 0, 0); line-height: normal;"><span lang=
=3D"EN-US"><font size=3D"2" face=3D"arial, sans-serif"><br></font></span></=
p><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0.0001pt; color: rgb(0, 0=
, 0); line-height: normal;"><font face=3D"arial, sans-serif"><span lang=3D"=
EN-US"></span></font></p><div><font face=3D"arial, sans-serif">and then use=
 it to initialize the vector:</font></div><div><font face=3D"arial, sans-se=
rif"><br></font></div><div><font face=3D"arial, sans-serif">&nbsp; &nbsp; v=
ector&lt;Point3d&gt; &nbsp; pts(1000,&nbsp;<span style=3D"color: rgb(0, 0, =
0); font-size: small;">minimal_construct())</span><span style=3D"color: rgb=
(0, 0, 0); font-size: small;">;</span></font></div><div><font face=3D"arial=
, sans-serif"><br></font></div><div><span style=3D"font-family: arial, sans=
-serif;">This will not work for many kinds of classes - any data that requi=
res&nbsp;</span><span style=3D"font-family: arial, sans-serif;">proper&nbsp=
;</span></div><div><span style=3D"font-family: arial, sans-serif;">initiali=
zation (e.g. strings, pointers) would invalidate this pattern.</span></div>=
</div></div><div><font face=3D"arial, sans-serif"><br></font></div><div><fo=
nt face=3D"arial, sans-serif">Best regards,</font></div><div><font face=3D"=
arial, sans-serif">Henrik</font></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_2581_1354020232.1417858037089--
------=_Part_2580_1179171364.1417858037088--

.