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 </font></div><div><font face=3D"ar=
ial, sans-serif">a </font><font face=3D"arial, sans-serif">different t=
ype </font><span style=3D"font-family: arial, sans-serif;">from which =
its </span><span style=3D"font-family: arial, sans-serif;">value_type =
is constructible from. </span><span style=3D"font-family: arial, sans-=
serif;">This is </span></div><div><span style=3D"font-family: arial, s=
ans-serif;">already available for a single </span><span style=3D"font-=
family: arial, sans-serif;">value using the vector::emplace_back </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 </font><span style=3D"font-family: arial, sans-serif;">t=
hat </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"> const vector<A> -> vector<B></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=
<B> </font><span style=3D"font-family: arial, sans-serif;">may c=
ause </span></div><div><span style=3D"font-family: arial, sans-serif;"=
>lots of memory writes that sometimes can be avoided. E.g. my well </s=
pan></div><div><font face=3D"arial, sans-serif">behaved 3D points </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);"> 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"> =
Point3d(<span style=3D"color: blue;">double</span>&nb=
sp;x_=3D0, <span style=3D"color: blue;">double</span> y_=3D0,&nbs=
p;<span style=3D"color: blue;">double</span> z_=3D0) : x(x_), y(y_), z=
(z_) {}<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"> Point3d(<span style=3D"co=
lor: blue;">const</span> minimal_construct &) &nb=
sp; {}<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"> v=
ector<Point3d> pts(1000, <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 </span><span style=3D"font-family: arial, sans-serif;">proper =
;</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" 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--
.