Topic: Names & Types for Structured Bindings


Author: Derek Ross <antiquarktv@gmail.com>
Date: Mon, 5 Dec 2016 08:24:15 -0800 (PST)
Raw View
------=_Part_2424_596653164.1480955055632
Content-Type: multipart/alternative;
 boundary="----=_Part_2425_176354449.1480955055633"

------=_Part_2425_176354449.1480955055633
Content-Type: text/plain; charset=UTF-8

(This is an expansion on my previous post, "Structurally Bound Aliases".)

Structured bindings allow us to separate out bound results into local
variables. However, if we have multiple similar structured bindings, we
have the problem of naming the elements to avoid collisions.
For example:

 map<int,string> mapA, mapB, mapC;
 auto [iterA, successB] = mapA.insert(value);
 auto [iterB, successB] = mapB.insert(value);
 auto [iterC, successC] = mapC.insert(value);
 cout << successA << " " << successB << " " << successC << endl;

If we could give a name to each structured binding, to allow access to the
elements as if the name represented a struct, then we could re-use element
names:

 auto [iter, success] retA = mapA.insert(value);
 auto [iter, success] retB = mapB.insert(value);
 auto [iter, success] retC = mapC.insert(value);
 cout << retA.success << " " << retB.success << " " << retC.success << endl;

Now that we have a name for a structured binding, we can use decltype to
convert that name to a type.
Then we can avoid repeatedly writing the [iter,success] text.

 auto [iter, success] retA = mapA.insert(value);
 decltype(retA) retB = mapB.insert(value);
 decltype(retA) retC = mapC.insert(value);

With a slight syntax modification, we can specify the name after the auto
keyword and avoid the decltype syntax:

 auto ret_t [iter, success] retA = mapA.insert(value);
 ret_t retB = mapB.insert(value);
 ret_t retC = mapC.insert(value);

But it's somewhat clumsy to require the initial mapA.insert call to produce
the ret_t type. Why not create an alias beforehand?

 using auto ret_t [iter, success] = decltype(mapA.insert(pair<int,string
>()));
 ret_t retA = mapA.insert(value);
 ret_t retB = mapB.insert(value);
 ret_t retC = mapC.insert(value);

The obvious question is, what is the type of ret_t?

In the above example, the compiler has created a type is almost identical
to a pair<iterator,bool>.
The differences are:

   - The .first and .second members have been replaced with .iter and
   .success members.
   - The new type has extra constructors so it can be initialized with a
   pair<iterator,bool>.

Since ret_t shares many of the member functions and operators with
std::pair, that means that these structured binding objects can be
compared, put into containers, etc.

Thanks,
Derek

--
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/e04925af-6391-4c7b-b2c5-7a7ff16fd72c%40isocpp.org.

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

<div dir=3D"ltr"><div>(This is an expansion on my previous post, &quot;Stru=
cturally Bound Aliases&quot;.)</div><div><br></div><div>Structured bindings=
 allow us to separate out bound results into local variables. However, if w=
e have multiple similar structured bindings, we have the problem of naming =
the elements to avoid collisions.=C2=A0</div><div>For example:</div><div><b=
r></div><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187,=
 187); word-wrap: break-word; background-color: rgb(250, 250, 250);"><code =
class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #=
000;" class=3D"styled-by-prettify">=C2=A0map</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" c=
lass=3D"styled-by-prettify">int</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">,</span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">string</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> mapA</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> mapB</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> mapC</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: #=
008;" class=3D"styled-by-prettify">auto</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">iterA</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> su=
ccessB</span><span style=3D"color: #660;" class=3D"styled-by-prettify">]</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> mapA</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">insert</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">value</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br>=C2=A0</span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">auto</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">iterB</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> successB</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">]</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> mapB</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">insert</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">value</sp=
an><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: #008;" class=3D"styled-by-prettify">auto</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">iterC</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> successC</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">]</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> mapC</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">insert</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">value</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br>=C2=A0cout </span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> successA </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #080;" class=3D"sty=
led-by-prettify">&quot; &quot;</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> successB </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify">&q=
uot; &quot;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> successC =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> endl</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">;</span></div></c=
ode></div><div><br></div><div>If we could give a name to each structured bi=
nding, to allow access to the elements as if the name represented a struct,=
 then we could re-use element names:=C2=A0</div><div>=C2=A0</div><div class=
=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: =
break-word; background-color: rgb(250, 250, 250);"><code class=3D"prettypri=
nt"><div class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"sty=
led-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-pre=
ttify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">[<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">iter</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> success</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">]</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> retA </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> mapA</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">insert</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">value</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: #008;" class=3D"styled-by-prettify">auto</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">[</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">iter</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> success</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">]</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> retB </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> mapB</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">insert</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">value</span><span style=
=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"colo=
r: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">[</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">iter</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 success</span><span style=3D"color: #660;" class=3D"styled-by-prettify">]<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> retC </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> mapC</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">insert</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">value</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"><br>=C2=A0cout </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> retA</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">success </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #080;" class=3D"styled-by-prettify">&quot; &quo=
t;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> retB</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">success </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;" =
class=3D"styled-by-prettify">&quot; &quot;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> retC</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">success </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> endl</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>;</span></div></code></div><div><br></div><div>Now that we have a name for=
 a structured binding, we can use decltype to convert that name to a type.=
=C2=A0<br></div><div>Then we can avoid repeatedly writing the [iter,success=
] text.=C2=A0</div><div>=C2=A0</div><div class=3D"prettyprint" style=3D"bor=
der: 1px solid rgb(187, 187, 187); word-wrap: break-word; background-color:=
 rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprettypr=
int"><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"> </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">[</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">iter</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> success</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">]</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> retA </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> m=
apA</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">insert</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">value</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br>=C2=A0</span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">decltype</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">retA</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> retB </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> m=
apB</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">insert</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">value</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br>=C2=A0</span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">decltype</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">ret</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">A</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
retC </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> mapC</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">insert</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">value</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">);</span></div></code></div><div><br></div=
><div>With a slight syntax modification, we can specify the name after the =
auto keyword and avoid the decltype syntax:=C2=A0</div><div>=C2=A0</div><di=
v class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word=
-wrap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"pr=
ettyprint"><div class=3D"subprettyprint"><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"> ret_t </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">[</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">iter</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> success</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">]</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> retA </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> mapA</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">insert</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">value</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br>=C2=A0ret_t retB </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> mapB</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">insert</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">value</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br>=C2=A0ret_t retC </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> mapC</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">insert</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">value</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">);</span></div></code></div><div>=C2=A0<br></div><div>But it=
&#39;s somewhat clumsy to require the initial=C2=A0<span class=3D"styled-by=
-prettify" style=3D"color: rgb(0, 0, 0); background-color: rgb(250, 250, 25=
0);">mapA</span><span class=3D"styled-by-prettify" style=3D"color: rgb(102,=
 102, 0); background-color: rgb(250, 250, 250);">.</span><span class=3D"sty=
led-by-prettify" style=3D"color: rgb(0, 0, 0); background-color: rgb(250, 2=
50, 250);">insert=C2=A0</span>call to produce the ret_t type. Why not creat=
e an alias beforehand?=C2=A0</div><div>=C2=A0</div><div class=3D"prettyprin=
t" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; ba=
ckground-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">=C2=A0</span><span style=3D"color: #008;" class=3D"styled-by-prettify">u=
sing</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> ret_t </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">[</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">iter</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> success</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">]</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">decltype<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">mapA</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">insert</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">pair</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">int</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">,</span><span style=3D"color: #008;" class=3D"styled-by-prettify">s=
tring</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(=
)));</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
=C2=A0ret_t retA </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 mapA</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">insert</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">value</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br>=C2=A0ret_t retB </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> mapB</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">insert</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">value</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br>=C2=A0ret_t retC </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> mapC</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">insert</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">value</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">);</span></div></code></di=
v><div><br></div><div>The obvious question is, what is the type of ret_t?</=
div><div><br></div><div>In the above example, the compiler has created a ty=
pe is almost identical to a pair&lt;iterator,bool&gt;.=C2=A0</div><div>The =
differences are:=C2=A0</div><div><ul><li>The .first and .second members hav=
e been replaced with .iter and .success members.=C2=A0<br></li><li>The new =
type has extra constructors so it can be initialized with a pair&lt;iterato=
r,bool&gt;.=C2=A0<br></li></ul></div><div>Since ret_t shares many of the me=
mber functions and operators with std::pair, that means that these structur=
ed binding objects can be compared, put into containers, etc.=C2=A0<br></di=
v><div><br></div><div>Thanks,=C2=A0</div><div>Derek</div><div><br></div></d=
iv>

<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/e04925af-6391-4c7b-b2c5-7a7ff16fd72c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e04925af-6391-4c7b-b2c5-7a7ff16fd72c=
%40isocpp.org</a>.<br />

------=_Part_2425_176354449.1480955055633--

------=_Part_2424_596653164.1480955055632--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 5 Dec 2016 08:37:46 -0800 (PST)
Raw View
------=_Part_708_352296795.1480955866737
Content-Type: multipart/alternative;
 boundary="----=_Part_709_1937707926.1480955866738"

------=_Part_709_1937707926.1480955866738
Content-Type: text/plain; charset=UTF-8

On Monday, December 5, 2016 at 11:24:15 AM UTC-5, Derek Ross wrote:
>
> (This is an expansion on my previous post, "Structurally Bound Aliases".)
>
> Structured bindings allow us to separate out bound results into local
> variables. However, if we have multiple similar structured bindings, we
> have the problem of naming the elements to avoid collisions.
> For example:
>
>  map<int,string> mapA, mapB, mapC;
>  auto [iterA, successB] = mapA.insert(value);
>  auto [iterB, successB] = mapB.insert(value);
>  auto [iterC, successC] = mapC.insert(value);
>  cout << successA << " " << successB << " " << successC << endl;
>
> If we could give a name to each structured binding, to allow access to the
> elements as if the name represented a struct,
>

That's not what structured binding is for. It's for deconstructing a
"product type" into its constituent elements, which have names.

If you wanted to access the "product type" itself, you wouldn't be using
structured binding. It's an either/or thing; you use one or the other, not
both.


> then we could re-use element names:
>
>  auto [iter, success] retA = mapA.insert(value);
>  auto [iter, success] retB = mapB.insert(value);
>  auto [iter, success] retC = mapC.insert(value);
>  cout << retA.success << " " << retB.success << " " << retC.success <<
> endl;
>
> Now that we have a name for a structured binding,
>

But a "structured binding" is not a type. It's not an entity. Even if we
allowed this syntax to work, `decltype(retA)` would still be
`pair<Iterator, bool>`. The compiler would simply be doing some
behind-the-scenes magic to convert `retA.iter` into `retA.first`.

--
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/1de16b52-e062-457f-ad00-f2119b7bfeb1%40isocpp.org.

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

<div dir=3D"ltr">On Monday, December 5, 2016 at 11:24:15 AM UTC-5, Derek Ro=
ss wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: =
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div=
>(This is an expansion on my previous post, &quot;Structurally Bound Aliase=
s&quot;.)</div><div><br></div><div>Structured bindings allow us to separate=
 out bound results into local variables. However, if we have multiple simil=
ar structured bindings, we have the problem of naming the elements to avoid=
 collisions.=C2=A0</div><div>For example:</div><div><br></div><div style=3D=
"border:1px solid rgb(187,187,187);word-wrap:break-word;background-color:rg=
b(250,250,250)"><code><div><span style=3D"color:#000">=C2=A0map</span><span=
 style=3D"color:#660">&lt;</span><span style=3D"color:#008">int</span><span=
 style=3D"color:#660">,</span><span style=3D"color:#008">string</span><span=
 style=3D"color:#660">&gt;</span><span style=3D"color:#000"> mapA</span><sp=
an style=3D"color:#660">,</span><span style=3D"color:#000"> mapB</span><spa=
n style=3D"color:#660">,</span><span style=3D"color:#000"> mapC</span><span=
 style=3D"color:#660">;</span><span style=3D"color:#000"><br>=C2=A0</span><=
span style=3D"color:#008">auto</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">[</span><span style=3D"color:#000">iterA</span><spa=
n style=3D"color:#660">,</span><span style=3D"color:#000"> successB</span><=
span style=3D"color:#660">]</span><span style=3D"color:#000"> </span><span =
style=3D"color:#660">=3D</span><span style=3D"color:#000"> mapA</span><span=
 style=3D"color:#660">.</span><span style=3D"color:#000">insert</span><span=
 style=3D"color:#660">(</span><span style=3D"color:#000">value</span><span =
style=3D"color:#660">);</span><span style=3D"color:#000"><br>=C2=A0</span><=
span style=3D"color:#008">auto</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">[</span><span style=3D"color:#000">iterB</span><spa=
n style=3D"color:#660">,</span><span style=3D"color:#000"> successB</span><=
span style=3D"color:#660">]</span><span style=3D"color:#000"> </span><span =
style=3D"color:#660">=3D</span><span style=3D"color:#000"> mapB</span><span=
 style=3D"color:#660">.</span><span style=3D"color:#000">insert</span><span=
 style=3D"color:#660">(</span><span style=3D"color:#000">value</span><span =
style=3D"color:#660">);</span><span style=3D"color:#000"><br>=C2=A0</span><=
span style=3D"color:#008">auto</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">[</span><span style=3D"color:#000">iterC</span><spa=
n style=3D"color:#660">,</span><span style=3D"color:#000"> successC</span><=
span style=3D"color:#660">]</span><span style=3D"color:#000"> </span><span =
style=3D"color:#660">=3D</span><span style=3D"color:#000"> mapC</span><span=
 style=3D"color:#660">.</span><span style=3D"color:#000">insert</span><span=
 style=3D"color:#660">(</span><span style=3D"color:#000">value</span><span =
style=3D"color:#660">);</span><span style=3D"color:#000"><br>=C2=A0cout </s=
pan><span style=3D"color:#660">&lt;&lt;</span><span style=3D"color:#000"> s=
uccessA </span><span style=3D"color:#660">&lt;&lt;</span><span style=3D"col=
or:#000"> </span><span style=3D"color:#080">&quot; &quot;</span><span style=
=3D"color:#000"> </span><span style=3D"color:#660">&lt;&lt;</span><span sty=
le=3D"color:#000"> successB </span><span style=3D"color:#660">&lt;&lt;</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#080">&quot; &quo=
t;</span><span style=3D"color:#000"> </span><span style=3D"color:#660">&lt;=
&lt;</span><span style=3D"color:#000"> successC </span><span style=3D"color=
:#660">&lt;&lt;</span><span style=3D"color:#000"> endl</span><span style=3D=
"color:#660">;</span></div></code></div><div><br></div><div>If we could giv=
e a name to each structured binding, to allow access to the elements as if =
the name represented a struct,</div></div></blockquote><div><br>That&#39;s =
not what structured binding is for. It&#39;s for deconstructing a &quot;pro=
duct type&quot; into its constituent elements, which have names.<br><br>If =
you wanted to access the &quot;product type&quot; itself, you wouldn&#39;t =
be using structured binding. It&#39;s an either/or thing; you use one or th=
e other, not both.<br>=C2=A0</div><blockquote class=3D"gmail_quote" style=
=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: =
1ex;"><div dir=3D"ltr"><div>then we could re-use element names:=C2=A0</div>=
<div>=C2=A0</div><div style=3D"border:1px solid rgb(187,187,187);word-wrap:=
break-word;background-color:rgb(250,250,250)"><code><div><span style=3D"col=
or:#000">=C2=A0</span><span style=3D"color:#008">auto</span><span style=3D"=
color:#000"> </span><span style=3D"color:#660">[</span><span style=3D"color=
:#000">iter</span><span style=3D"color:#660">,</span><span style=3D"color:#=
000"> success</span><span style=3D"color:#660">]</span><span style=3D"color=
:#000"> retA </span><span style=3D"color:#660">=3D</span><span style=3D"col=
or:#000"> mapA</span><span style=3D"color:#660">.</span><span style=3D"colo=
r:#000">insert</span><span style=3D"color:#660">(</span><span style=3D"colo=
r:#000">value</span><span style=3D"color:#660">);</span><span style=3D"colo=
r:#000"><br>=C2=A0</span><span style=3D"color:#008">auto</span><span style=
=3D"color:#000"> </span><span style=3D"color:#660">[</span><span style=3D"c=
olor:#000">iter</span><span style=3D"color:#660">,</span><span style=3D"col=
or:#000"> success</span><span style=3D"color:#660">]</span><span style=3D"c=
olor:#000"> retB </span><span style=3D"color:#660">=3D</span><span style=3D=
"color:#000"> mapB</span><span style=3D"color:#660">.</span><span style=3D"=
color:#000">insert</span><span style=3D"color:#660">(</span><span style=3D"=
color:#000">value</span><span style=3D"color:#660">);</span><span style=3D"=
color:#000"><br>=C2=A0</span><span style=3D"color:#008">auto</span><span st=
yle=3D"color:#000"> </span><span style=3D"color:#660">[</span><span style=
=3D"color:#000">iter</span><span style=3D"color:#660">,</span><span style=
=3D"color:#000"> success</span><span style=3D"color:#660">]</span><span sty=
le=3D"color:#000"> retC </span><span style=3D"color:#660">=3D</span><span s=
tyle=3D"color:#000"> mapC</span><span style=3D"color:#660">.</span><span st=
yle=3D"color:#000">insert</span><span style=3D"color:#660">(</span><span st=
yle=3D"color:#000">value</span><span style=3D"color:#660">);</span><span st=
yle=3D"color:#000"><br>=C2=A0cout </span><span style=3D"color:#660">&lt;&lt=
;</span><span style=3D"color:#000"> retA</span><span style=3D"color:#660">.=
</span><span style=3D"color:#000">success </span><span style=3D"color:#660"=
>&lt;&lt;</span><span style=3D"color:#000"> </span><span style=3D"color:#08=
0">&quot; &quot;</span><span style=3D"color:#000"> </span><span style=3D"co=
lor:#660">&lt;&lt;</span><span style=3D"color:#000"> retB</span><span style=
=3D"color:#660">.</span><span style=3D"color:#000">success </span><span sty=
le=3D"color:#660">&lt;&lt;</span><span style=3D"color:#000"> </span><span s=
tyle=3D"color:#080">&quot; &quot;</span><span style=3D"color:#000"> </span>=
<span style=3D"color:#660">&lt;&lt;</span><span style=3D"color:#000"> retC<=
/span><span style=3D"color:#660">.</span><span style=3D"color:#000">success=
 </span><span style=3D"color:#660">&lt;&lt;</span><span style=3D"color:#000=
"> endl</span><span style=3D"color:#660">;</span></div></code></div><div><b=
r></div><div>Now that we have a name for a structured binding,</div></div><=
/blockquote><div><br>But a &quot;structured binding&quot; is not a type. It=
&#39;s not an entity. Even if we allowed this syntax to work, `decltype(ret=
A)` would still be `pair&lt;Iterator, bool&gt;`. The compiler would simply =
be doing some behind-the-scenes magic to convert `retA.iter` into `retA.fir=
st`.</div></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/1de16b52-e062-457f-ad00-f2119b7bfeb1%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1de16b52-e062-457f-ad00-f2119b7bfeb1=
%40isocpp.org</a>.<br />

------=_Part_709_1937707926.1480955866738--

------=_Part_708_352296795.1480955866737--

.


Author: Derek Ross <antiquarktv@gmail.com>
Date: Mon, 5 Dec 2016 08:57:25 -0800 (PST)
Raw View
------=_Part_4152_1797280645.1480957045339
Content-Type: multipart/alternative;
 boundary="----=_Part_4153_1154714538.1480957045339"

------=_Part_4153_1154714538.1480957045339
Content-Type: text/plain; charset=UTF-8



On Monday, December 5, 2016 at 10:37:46 AM UTC-6, Nicol Bolas wrote:
>
>
> That's not what structured binding is for. It's for deconstructing a
> "product type" into its constituent elements, which have names.
>

I see no downside to grouping together a collection of variables into a
struct-like object. Why shouldn't we be able to do that with a structured
binding?


>
> If you wanted to access the "product type" itself, you wouldn't be using
> structured binding. It's an either/or thing; you use one or the other, not
> both.
>

Well... why not? C++ is designed to be useful, why not give it useful
features?

>
>


> But a "structured binding" is not a type. It's not an entity.
>

Which explains why I posted this idea in "Future Proposals."


> `decltype(retA)` would still be `pair<Iterator, bool>`.
>

No it wouldn't, because the type "auto retA = map.insert" is not the same
type as "auto [i,s] retA = map.insert"


> The compiler would simply be doing some behind-the-scenes magic to convert
> `retA.iter` into `retA.first`.
>

Isn't the whole purpose of the compiler to do behind the scenes magic?
Like, since the 1950s?


--
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/e3cded9e-9cb2-4a3c-b818-84a7f5dbd236%40isocpp.org.

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

<div dir=3D"ltr"><br><br>On Monday, December 5, 2016 at 10:37:46 AM UTC-6, =
Nicol Bolas wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;marg=
in-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"=
ltr"><div><br>That&#39;s not what structured binding is for. It&#39;s for d=
econstructing a &quot;product type&quot; into its constituent elements, whi=
ch have names.<br></div></div></blockquote><div><br></div><div>I see no dow=
nside to grouping together a collection of variables into a struct-like obj=
ect. Why shouldn&#39;t we be able to do that with a structured binding?</di=
v><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;mar=
gin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D=
"ltr"><div><br>If you wanted to access the &quot;product type&quot; itself,=
 you wouldn&#39;t be using structured binding. It&#39;s an either/or thing;=
 you use one or the other, not both.<br></div></div></blockquote><div><br><=
/div><div>Well... why not? C++ is designed to be useful, why not give it us=
eful features?</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div>=C2=A0</div></div></blockquote><div>=C2=A0</div><blockquote c=
lass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px=
 #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div>But a &quot;structure=
d binding&quot; is not a type. It&#39;s not an entity.=C2=A0</div></div></b=
lockquote><div><br></div><div>Which explains why I posted this idea in &quo=
t;Future Proposals.&quot;=C2=A0<br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;"><div dir=3D"ltr"><div>`decltype(retA)` would st=
ill be `pair&lt;Iterator, bool&gt;`. </div></div></blockquote><div><br></di=
v><div>No it wouldn&#39;t, because the type &quot;auto retA =3D map.insert&=
quot; is not the same type as &quot;auto [i,s] retA =3D map.insert&quot;</d=
iv><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div>The compiler would simply be doing some behind-the-scenes mag=
ic to convert `retA.iter` into `retA.first`.</div></div></blockquote><div><=
br></div><div>Isn&#39;t the whole purpose of the compiler to do behind the =
scenes magic? Like, since the 1950s?</div><div>=C2=A0</div></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/e3cded9e-9cb2-4a3c-b818-84a7f5dbd236%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e3cded9e-9cb2-4a3c-b818-84a7f5dbd236=
%40isocpp.org</a>.<br />

------=_Part_4153_1154714538.1480957045339--

------=_Part_4152_1797280645.1480957045339--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 5 Dec 2016 10:04:25 -0800 (PST)
Raw View
------=_Part_1941_19103524.1480961065735
Content-Type: multipart/alternative;
 boundary="----=_Part_1942_1312045359.1480961065736"

------=_Part_1942_1312045359.1480961065736
Content-Type: text/plain; charset=UTF-8

On Monday, December 5, 2016 at 11:57:25 AM UTC-5, Derek Ross wrote:
>
> On Monday, December 5, 2016 at 10:37:46 AM UTC-6, Nicol Bolas wrote:
>>
>>
>> That's not what structured binding is for. It's for deconstructing a
>> "product type" into its constituent elements, which have names.
>>
>
> I see no downside to grouping together a collection of variables into a
> struct-like object. Why shouldn't we be able to do that with a structured
> binding?
>

Because that's not what structured binding is for. Or even how structured
binding *works*.

If you do this:

auto [iter, success] = map.insert(...);

`iter` and `success` are basically references (though `decltype(iter)` will
not be a reference type in this case). The compiler is given the freedom to
replace uses of `iter` and `success` with `a.first` and `a.second`, where
`a` is the return value from `map.insert`.

That's what structured binding is all about. SB is not about *creating
types* from other types; it doesn't even create objects from other objects.
So if you want some syntax for create types from other types, structured
binding should not be the basis of that syntax. That would be appropriating
syntax for a very different purpose.

If you wanted to access the "product type" itself, you wouldn't be using
>> structured binding. It's an either/or thing; you use one or the other, not
>> both.
>>
>
> Well... why not? C++ is designed to be useful, why not give it useful
> features?
>

Because the is little genuine *need* for such a thing.

Structured binding as a feature justifies itself in part by the fact that
there are times when a function needs to return multiple values, and the
receiving code would be a lot more clear if it didn't have a bunch of
`get<I>` calls to access those members. This is a well-understood problem
that many users have, and structured binding solves it (among other things)
in a way that *could not* be adequately resolved without a language feature.

We don't add features to the language because someone declares that they
are useful. Because someone *could* use it in some way. We add language
features because they solve fairly widespread problems that could not
adequately be solved in other ways. `auto` solves the problem of using
complex types in template code (among others). Lambdas solve the problem of
being able to use callbacks and algorithms without sacrificing code
locality (among others). Deduced return values solves the `auto` problem
for return values. Concepts makes SFINAE not require a degree in advanced
metaprogramming. And so forth.

These are all problems lots of programmers face that could not otherwise be
solved *without* being a language feature. And in many cases, we *really*
tried to solve them within the language. Boost.Lambda was a good-faith try
at making lambdas work without language support. `enable_if` was an attempt
to get concepts-like functionality without a language feature. And so
forth. Those efforts proved that you could not adequately solve those
problems without language support.

The problem you present here can be adequately solved by using more
descriptive variable names. Indeed, the ability to access the product type
and its components can be adequately solved like this:

auto p = map.insert(...);
auto & [iter, success] = p;

This doesn't create a new type or anything. But it does allow you to decide
to access the product type or the components, as you see fit.

--
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/24666bd9-7873-4645-88be-ea8e71b3c5b1%40isocpp.org.

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

<div dir=3D"ltr">On Monday, December 5, 2016 at 11:57:25 AM UTC-5, Derek Ro=
ss wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: =
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">On M=
onday, December 5, 2016 at 10:37:46 AM UTC-6, Nicol Bolas wrote:<blockquote=
 class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px =
#ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br>That&#39;s not what =
structured binding is for. It&#39;s for deconstructing a &quot;product type=
&quot; into its constituent elements, which have names.<br></div></div></bl=
ockquote><div><br></div><div>I see no downside to grouping together a colle=
ction of variables into a struct-like object. Why shouldn&#39;t we be able =
to do that with a structured binding?</div></div></blockquote><div><br>Beca=
use that&#39;s not what structured binding is for. Or even how structured b=
inding <i>works</i>.<br><br>If you do this:<br><br><div style=3D"background=
-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style:=
 solid; border-width: 1px; overflow-wrap: break-word;" class=3D"prettyprint=
"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"=
color: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">[</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">iter</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> success</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">=3D</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> map</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">insert</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(...);</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"><br></span></div></code></div><br>`iter` and =
`success` are basically references (though `decltype(iter)` will not be a r=
eference type in this case). The compiler is given the freedom to replace u=
ses of `iter` and `success` with `a.first` and `a.second`, where `a` is the=
 return value from `map.insert`.<br><br>That&#39;s what structured binding =
is all about. SB is not about <i>creating types</i> from other types; it do=
esn&#39;t even create objects from other objects. So if you want some synta=
x for create types from other types, structured binding should not be the b=
asis of that syntax. That would be appropriating syntax for a very differen=
t purpose.<br><br></div><blockquote class=3D"gmail_quote" style=3D"margin: =
0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div d=
ir=3D"ltr"><div></div><blockquote class=3D"gmail_quote" style=3D"margin:0;m=
argin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"l=
tr"><div>If you wanted to access the &quot;product type&quot; itself, you w=
ouldn&#39;t be using structured binding. It&#39;s an either/or thing; you u=
se one or the other, not both.<br></div></div></blockquote><div><br></div><=
div>Well... why not? C++ is designed to be useful, why not give it useful f=
eatures?<br></div></div></blockquote><div><br>Because the is little genuine=
 <i>need</i> for such a thing.<br><br>Structured binding as a feature justi=
fies itself in part by the fact=20
that there are times when a function needs to return multiple values,=20
and the receiving code would be a lot more clear if it didn&#39;t have a=20
bunch of `get&lt;I&gt;` calls to access those members. This is a=20
well-understood problem that many users have, and structured binding=20
solves it (among other things) in a way that <i>could not</i> be adequately=
 resolved without a language feature.<br><br>We don&#39;t add features to t=
he language because someone declares that they are useful. Because someone =
<i>could</i> use it in some way. We add language features because they solv=
e fairly widespread problems that could not adequately be solved in other w=
ays. `auto` solves the problem of using complex types in template code (amo=
ng others). Lambdas solve the problem of being able to use callbacks and al=
gorithms without sacrificing code locality (among others). Deduced return v=
alues solves the `auto` problem for return values. Concepts makes SFINAE no=
t require a degree in advanced metaprogramming. And so forth.<br><br>These =
are all problems lots of programmers face that could not otherwise be solve=
d <i>without</i> being a language feature. And in many cases, we <i>really<=
/i> tried to solve them within the language. Boost.Lambda was a good-faith =
try at making lambdas work without language support. `enable_if` was an att=
empt to get concepts-like functionality without a language feature. And so =
forth. Those efforts proved that you could not adequately solve those probl=
ems without language support.<br><br>The problem you present here can be ad=
equately solved by using more descriptive
 variable names. Indeed, the ability to access the product type and its com=
ponents can be adequately solved like this:<br><br><div style=3D"background=
-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style:=
 solid; border-width: 1px; overflow-wrap: break-word;" class=3D"prettyprint=
"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"=
color: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> p </span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> map</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">insert</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">(...);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">[</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">iter</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> success</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">]</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> p</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div></code></d=
iv><br>This doesn&#39;t create a new type or anything. But it does allow yo=
u to decide to access the product type or the components, as you see fit.<b=
r></div></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/24666bd9-7873-4645-88be-ea8e71b3c5b1%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/24666bd9-7873-4645-88be-ea8e71b3c5b1=
%40isocpp.org</a>.<br />

------=_Part_1942_1312045359.1480961065736--

------=_Part_1941_19103524.1480961065735--

.