Topic: Default initialization of standard containers


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 12 Nov 2012 17:47:17 -0800 (PST)
Raw View
------=_Part_756_14734571.1352771237612
Content-Type: text/plain; charset=ISO-8859-1

C++ operates on a "pay only for what you use" design (usually). That's why
we have a difference between value initialization and default
initialization.

The problem is that you cannot use default initialization in a container.
It's either value initialization or copy initialization. This proposal is
to add a few constructors to allow this:

std::vector<T> valueInit(42);
std::vector<T> copyInit(42, T{...});
std::vector<T> defaultInit(42, std::default_init);

`std::default_init` is a global value of type `std::default_init_t`, which
is just an empty struct. This will select the constructor that will default
construct the members of the container.

This can be combined with my "more uniform initialization" proposal:

std::vector<T> valueInit{42, std::value_init};
std::vector<T> copyInit{42, T{...}};
std::vector<T> defaultInit{42, std::default_init};

This is most useful for trivial structs/classes, as well as allocating
space for memory buffers, like with a `vector<char>`. No point in zero-ing
out all of that memory if you're just going to turn it around and use it to
make the dolls ready.

--




------=_Part_756_14734571.1352771237612
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

C++
 operates on a "pay only for what you use" design (usually). That's why=20
we have a difference between value initialization and default=20
initialization.<br><br>The problem is that you cannot use default initializ=
ation in a container. It's either value initialization or copy initializati=
on. This proposal is to add a few constructors to allow this:<br><br><div c=
lass=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-=
color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wra=
p: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><=
span style=3D"color: #000;" class=3D"styled-by-prettify">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=
: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">T</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> valueInit</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #066;" class=3D"styled-by-p=
rettify">42</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>std=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">T</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> copyInit</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">(</span><span style=3D"color: #066;" class=
=3D"styled-by-prettify">42</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">,</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"><br>=
std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">T</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> defaultInit</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #066;" c=
lass=3D"styled-by-prettify">42</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> std</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">def=
ault_init</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
;</span></div></code></div><br>`std::default_init` is a global value of typ=
e `std::default_init_t`, which is just an empty struct. This will select th=
e constructor that will default construct the members of the container.<br>=
<br>This can be combined with my "more uniform initialization" proposal:<br=
><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 25=
0); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1p=
x; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpre=
ttyprint"><span style=3D"color: #000;" class=3D"styled-by-prettify">std</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">T</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> valueInit</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">{</span><span style=3D"color: #066;" class=3D"s=
tyled-by-prettify">42</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">value_init</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">};</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br>std</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">vector</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify">T</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> copyInit</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">{</span><span style=3D"color: #066;" class=3D"styled-by-p=
rettify">42</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">{...}};</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br>std</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">vector</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify">T</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> defaultInit</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">{</span><span style=3D"color: #066;" class=3D"sty=
led-by-prettify">42</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">default_init</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">};</span></d=
iv></code></div><br>This
 is most useful for trivial structs/classes, as well as allocating space
 for memory buffers, like with a `vector&lt;char&gt;`. No point in=20
zero-ing out all of that memory if you're just going to turn it around=20
and use it to make the dolls ready.<br>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_756_14734571.1352771237612--

.


Author: Arthur Tchaikovsky <atch.cpp@gmail.com>
Date: Mon, 12 Nov 2012 22:59:18 -0800 (PST)
Raw View
------=_Part_2043_5358289.1352789958247
Content-Type: text/plain; charset=ISO-8859-1

Again, I'm interested like the most common/default init with few values
would look like according to your proposal. Could you give some examples?

On Tuesday, 13 November 2012 01:47:17 UTC, Nicol Bolas wrote:
>
> C++ operates on a "pay only for what you use" design (usually). That's why
> we have a difference between value initialization and default
> initialization.
>
> The problem is that you cannot use default initialization in a container.
> It's either value initialization or copy initialization. This proposal is
> to add a few constructors to allow this:
>
> std::vector<T> valueInit(42);
> std::vector<T> copyInit(42, T{...});
> std::vector<T> defaultInit(42, std::default_init);
>
> `std::default_init` is a global value of type `std::default_init_t`, which
> is just an empty struct. This will select the constructor that will default
> construct the members of the container.
>
> This can be combined with my "more uniform initialization" proposal:
>
> std::vector<T> valueInit{42, std::value_init};
> std::vector<T> copyInit{42, T{...}};
> std::vector<T> defaultInit{42, std::default_init};
>
> This is most useful for trivial structs/classes, as well as allocating
> space for memory buffers, like with a `vector<char>`. No point in zero-ing
> out all of that memory if you're just going to turn it around and use it to
> make the dolls ready.
>

--




------=_Part_2043_5358289.1352789958247
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Again, I'm interested like the most common/default init with few values wou=
ld look like according to your proposal. Could you give some examples?<br><=
br>On Tuesday, 13 November 2012 01:47:17 UTC, Nicol Bolas  wrote:<blockquot=
e class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: =
1px #ccc solid;padding-left: 1ex;">C++
 operates on a "pay only for what you use" design (usually). That's why=20
we have a difference between value initialization and default=20
initialization.<br><br>The problem is that you cannot use default initializ=
ation in a container. It's either value initialization or copy initializati=
on. This proposal is to add a few constructors to allow this:<br><br><div s=
tyle=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bor=
der-style:solid;border-width:1px;word-wrap:break-word"><code><div><span sty=
le=3D"color:#000">std</span><span style=3D"color:#660">::</span><span style=
=3D"color:#000">vector</span><span style=3D"color:#660">&lt;</span><span st=
yle=3D"color:#000">T</span><span style=3D"color:#660">&gt;</span><span styl=
e=3D"color:#000"> valueInit</span><span style=3D"color:#660">(</span><span =
style=3D"color:#066">42</span><span style=3D"color:#660">);</span><span sty=
le=3D"color:#000"><br>std</span><span style=3D"color:#660">::</span><span s=
tyle=3D"color:#000">vector</span><span style=3D"color:#660">&lt;</span><spa=
n style=3D"color:#000">T</span><span style=3D"color:#660">&gt;</span><span =
style=3D"color:#000"> copyInit</span><span style=3D"color:#660">(</span><sp=
an style=3D"color:#066">42</span><span style=3D"color:#660">,</span><span s=
tyle=3D"color:#000"> T</span><span style=3D"color:#660">{...});</span><span=
 style=3D"color:#000"><br>std</span><span style=3D"color:#660">::</span><sp=
an style=3D"color:#000">vector</span><span style=3D"color:#660">&lt;</span>=
<span style=3D"color:#000">T</span><span style=3D"color:#660">&gt;</span><s=
pan style=3D"color:#000"> defaultInit</span><span style=3D"color:#660">(</s=
pan><span style=3D"color:#066">42</span><span style=3D"color:#660">,</span>=
<span style=3D"color:#000"> std</span><span style=3D"color:#660">::</span><=
span style=3D"color:#000">default_init</span><span style=3D"color:#660">);<=
/span></div></code></div><br>`std::default_init` is a global value of type =
`std::default_init_t`, which is just an empty struct. This will select the =
constructor that will default construct the members of the container.<br><b=
r>This can be combined with my "more uniform initialization" proposal:<br><=
br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187=
,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div>=
<span style=3D"color:#000">std</span><span style=3D"color:#660">::</span><s=
pan style=3D"color:#000">vector</span><span style=3D"color:#660">&lt;</span=
><span style=3D"color:#000">T</span><span style=3D"color:#660">&gt;</span><=
span style=3D"color:#000"> valueInit</span><span style=3D"color:#660">{</sp=
an><span style=3D"color:#066">42</span><span style=3D"color:#660">,</span><=
span style=3D"color:#000"> std</span><span style=3D"color:#660">::</span><s=
pan style=3D"color:#000">value_init</span><span style=3D"color:#660">};</sp=
an><span style=3D"color:#000"><br>std</span><span style=3D"color:#660">::</=
span><span style=3D"color:#000">vector</span><span style=3D"color:#660">&lt=
;</span><span style=3D"color:#000">T</span><span style=3D"color:#660">&gt;<=
/span><span style=3D"color:#000"> copyInit</span><span style=3D"color:#660"=
>{</span><span style=3D"color:#066">42</span><span style=3D"color:#660">,</=
span><span style=3D"color:#000"> T</span><span style=3D"color:#660">{...}};=
</span><span style=3D"color:#000"><br>std</span><span style=3D"color:#660">=
::</span><span style=3D"color:#000">vector</span><span style=3D"color:#660"=
>&lt;</span><span style=3D"color:#000">T</span><span style=3D"color:#660">&=
gt;</span><span style=3D"color:#000"> defaultInit</span><span style=3D"colo=
r:#660">{</span><span style=3D"color:#066">42</span><span style=3D"color:#6=
60">,</span><span style=3D"color:#000"> std</span><span style=3D"color:#660=
">::</span><span style=3D"color:#000">default_init</span><span style=3D"col=
or:#660">};</span></div></code></div><br>This
 is most useful for trivial structs/classes, as well as allocating space
 for memory buffers, like with a `vector&lt;char&gt;`. No point in=20
zero-ing out all of that memory if you're just going to turn it around=20
and use it to make the dolls ready.<br></blockquote>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_2043_5358289.1352789958247--

.


Author: Nevin Liber <nevin@eviloverlord.com>
Date: Tue, 13 Nov 2012 01:39:16 -0600
Raw View
--bcaec553fdde41eb6f04ce5b8615
Content-Type: text/plain; charset=ISO-8859-1

On 12 November 2012 19:47, Nicol Bolas <jmckesson@gmail.com> wrote:

> `std::default_init` is a global value of type `std::default_init_t`, which
> is just an empty struct. This will select the constructor that will default
> construct the members of the container.
>
> This can be combined with my "more uniform initialization" proposal:
>
> std::vector<T> valueInit{42, std::value_init};
> std::vector<T> copyInit{42, T{...}};
> std::vector<T> defaultInit{42, std::default_init};
>

Are containers required to use their allocator::construct method, or are
they allowed to use placement new directly?  If it is the former,
default_init has a bigger impact.


> --

 Nevin ":-)" Liber  <mailto:nevin@eviloverlord.com>  (847) 691-1404

--




--bcaec553fdde41eb6f04ce5b8615
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 12 November 2012 19:47, Nicol Bolas <span dir=3D"ltr">&lt;<a href=3D"mai=
lto:jmckesson@gmail.com" target=3D"_blank">jmckesson@gmail.com</a>&gt;</spa=
n> wrote:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

`std::default_init` is a global value of type `std::default_init_t`, which =
is just an empty struct. This will select the constructor that will default=
 construct the members of the container.<br><br>This can be combined with m=
y &quot;more uniform initialization&quot; proposal:<br>

<br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style>std</span><span style=3D"color:#660">::</span><span style>vect=
or</span><span style=3D"color:#660">&lt;</span><span style>T</span><span st=
yle=3D"color:#660">&gt;</span><span style> valueInit</span><span style=3D"c=
olor:#660">{</span><span style=3D"color:#066">42</span><span style=3D"color=
:#660">,</span><span style> std</span><span style=3D"color:#660">::</span><=
span style>value_init</span><span style=3D"color:#660">};</span><span style=
><br>

std</span><span style=3D"color:#660">::</span><span style>vector</span><spa=
n style=3D"color:#660">&lt;</span><span style>T</span><span style=3D"color:=
#660">&gt;</span><span style> copyInit</span><span style=3D"color:#660">{</=
span><span style=3D"color:#066">42</span><span style=3D"color:#660">,</span=
><span style> T</span><span style=3D"color:#660">{...}};</span><span style>=
<br>

std</span><span style=3D"color:#660">::</span><span style>vector</span><spa=
n style=3D"color:#660">&lt;</span><span style>T</span><span style=3D"color:=
#660">&gt;</span><span style> defaultInit</span><span style=3D"color:#660">=
{</span><span style=3D"color:#066">42</span><span style=3D"color:#660">,</s=
pan><span style> std</span><span style=3D"color:#660">::</span><span style>=
default_init</span><span style=3D"color:#660">};</span></div>

</code></div></blockquote><div><br></div><div>Are containers required to us=
e their allocator::construct method, or are they allowed to use placement n=
ew directly? =A0If it is the former, default_init has a bigger impact.</div=
>

<div>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;=
border-left:1px #ccc solid;padding-left:1ex">--=A0</blockquote></div>=A0Nev=
in &quot;:-)&quot; Liber=A0 &lt;mailto:<a href=3D"mailto:nevin@eviloverlord=
..com" target=3D"_blank">nevin@eviloverlord.com</a>&gt;=A0 (847) 691-1404<br=
>


<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

--bcaec553fdde41eb6f04ce5b8615--

.