Topic: Initial thoughts on an "initialization function" proposal


Author: =?UTF-8?Q?Jonathan_M=c3=bcller?= <jonathanmueller.dev@gmail.com>
Date: Mon, 11 Sep 2017 22:29:26 +0200
Raw View
On 11.09.2017 21:56, Nasos wrote:
> The *motivation* of the proposal is to provide a way to initialize stl
> containers using a function that operates on uninitialized data. The
> reason for that is that for certain applications, default or trivial
> initialization may hinder performance because the user will need to
> initialize memory twice: one for default initialization and another one
> to set the container data to the desired values (usually a complex
> function). At the same time, workarounds can become pretty involved,
> with the need to operate on uninitialized memory for longer than needed
> time periods, or with syntax that is unnecessarily complex.

The following does almost what you want:

|
std::vector<T> vec;
vec.reserve(size);
std::generate_n(std::back_inserter(vec), size, func);
|

The only downside is that `func` doesn't receive the current index, so
this might be interesting to propose.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f4d38c19-eaac-52a9-f1d8-a88bd46e8e78%40gmail.com.

.


Author: Nasos <steve.rodeen@gmail.com>
Date: Mon, 11 Sep 2017 12:56:01 -0700 (PDT)
Raw View
------=_Part_2725_82086762.1505159761390
Content-Type: multipart/alternative;
 boundary="----=_Part_2726_1306515534.1505159761391"

------=_Part_2726_1306515534.1505159761391
Content-Type: text/plain; charset="UTF-8"

Hello all,

I am posting to receive an initial feedback on a possible future standard
proposal.

The *motivation* of the proposal is to provide a way to initialize stl
containers using a function that operates on uninitialized data. The reason
for that is that for certain applications, default or trivial
initialization may hinder performance because the user will need to
initialize memory twice: one for default initialization and another one to
set the container data to the desired values (usually a complex function).
At the same time, workarounds can become pretty involved, with the need to
operate on uninitialized memory for longer than needed time periods, or
with syntax that is unnecessarily complex.

The interface I am imaging will provide a constructor that accepts a size
argument and a function. Here is some pseudocode for an "initialization
function" vector constructor:

template <typename T, ... >
class vector
{
public:
....
vector( const size_type &s, Func &&f )
{
  allocate memory to hold s objects of type T (do not initialize)
  ...

  size_type i = 0;
  for (  auto v&;  *this )
  {
    f ( i++, v );
  }
}
....


Such an implementation would provide functionality like the following:
std::size_t size = 100;

std::vector<double> v( size, [](auto i, double &e)
{
  e = i * i; // Setting  the "i"th element of vector "v" to i*i
});


The functionality I am imagining would be similar to the one provided by
std::initializer_list.I believe using the intializer_list interface, what I
am proposing can be implicitly implemented already (in a "functor" type of
approach).

It can be expected that the user may fail to initialize the data, by for
example providing an empty function body, but this check can possibly be
deferred to the compiler.

I am eager to hear your thoughts comments and criticism.

Thanks in advance!
-Nasos

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/d2641e55-ca54-4e02-b3da-b5f8afc0ce12%40isocpp.org.

------=_Part_2726_1306515534.1505159761391
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello all,<br><br>I am posting to receive an initial feedb=
ack on a possible future standard proposal. <br><br>The <b>motivation</b> o=
f the proposal is to provide a way to initialize stl containers using a fun=
ction that operates on uninitialized data. The reason for that is that for =
certain applications, default or trivial initialization may hinder performa=
nce because the user will need to initialize memory twice: one for default =
initialization and another one to set the container data to the desired val=
ues (usually a complex function). At the same time, workarounds can become =
pretty involved, with the need to operate on uninitialized memory for longe=
r than needed time periods, or with syntax that is unnecessarily complex. <=
br>=C2=A0<br>The interface I am imaging will provide a constructor that acc=
epts a size argument and a function. Here is some pseudocode for an &quot;i=
nitialization function&quot; vector constructor:<br><br><span style=3D"font=
-family: courier new,monospace;"><div style=3D"background-color: rgb(250, 2=
50, 250); border-color: rgb(187, 187, 187); border-style: solid; border-wid=
th: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><code class=3D"p=
rettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">template</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">...</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" c=
lass=3D"styled-by-prettify">class</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> vector<br></span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">public</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
></span><span style=3D"color: #660;" class=3D"styled-by-prettify">...</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>vector</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> size_type </span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">s</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">Func</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;&amp=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">f </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br>=C2=A0 allocate memory to hold s obje=
cts of type T </span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #008;" class=3D"styled-by-prettify">do</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">not</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> initialize</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">...</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> <br><br>=C2=A0 size_type i </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #=
066;" class=3D"styled-by-prettify">0</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br>=C2=A0 </span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">for</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> v</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&amp;;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">*</span><span style=3D"color: #=
008;" class=3D"styled-by-prettify">this</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br>=C2=A0 </span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br>=C2=A0 =C2=A0 f </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> i</span><span style=3D"color: #660;" class=3D"styled-by-prettify">++,<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> v </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">...</span></div></code></div><br></span><br>Such an imp=
lementation would provide functionality like the following:<br><font face=
=3D"courier new,monospace"><div style=3D"background-color: rgb(250, 250, 25=
0); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1p=
x; overflow-wrap: break-word;" class=3D"prettyprint"><code class=3D"prettyp=
rint"><div class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">std</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">size_t size </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #066;" class=3D"styled-by-prettify">100</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br><br>std</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">vector</span><span style=3D"color=
: #080;" class=3D"styled-by-prettify">&lt;double&gt;</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> v</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> size</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">[](</span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> i</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">double</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">e</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 e =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> i </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">*</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> i</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styl=
ed-by-prettify">// Setting =C2=A0the &quot;i&quot;th element of vector &quo=
t;v&quot; to i*i</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
});</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></s=
pan></div></code></div><br></font>=C2=A0<br>The functionality I am imaginin=
g would be similar to the one provided by <span style=3D"font-family: couri=
er new,monospace;">std::initializer_list</span>.I believe using the <span s=
tyle=3D"font-family: georgia,serif;">intializer_list</span> interface, what=
 I am proposing can be implicitly implemented already (in a &quot;functor&q=
uot; type of approach).<br><br>It can be expected that the user may fail to=
 initialize the data, by for example providing an empty function body, but =
this check can possibly be deferred to the compiler.<br><br>I am eager to h=
ear your thoughts comments and criticism.<br><br>Thanks in advance!<br>-Nas=
os<br><br></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/d2641e55-ca54-4e02-b3da-b5f8afc0ce12%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/d2641e55-ca54-4e02-b3da-b5f8afc0ce12=
%40isocpp.org</a>.<br />

------=_Part_2726_1306515534.1505159761391--

------=_Part_2725_82086762.1505159761390--

.