Topic: Has applying a template to multiple statements


Author: schreiber.corentin@gmail.com
Date: Sun, 29 Jan 2017 03:01:21 -0800 (PST)
Raw View
------=_Part_984_194142502.1485687681748
Content-Type: multipart/alternative;
 boundary="----=_Part_985_218977819.1485687681748"

------=_Part_985_218977819.1485687681748
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Le samedi 28 janvier 2017 09:26:29 UTC+1, eya...@tx.technion.ac.il a =C3=A9=
crit :
>
> (I've moved this question from=20
> http://stackoverflow.com/questions/41904201/ as per suggestions there.)
>
> In C++, we currently have to template function and variable definitions=
=20
> individually:
>
> template <typename T> void foo() { /* ... */ }
> template <typename T> void bar() { /* ... */ }
> template <typename T> std::vector<T> baz;
>
> well, I would expect the template keyword to be able to apply to braced=
=20
> scopse, similarly to namespaces, so that I could write:
>
> template <typename T> {
> void foo() { /* ... */ }
> void bar() { /* ... */ }
> std::vector<T> baz;
> }
>
> or at least
>
> template <typename T> {
> void foo<T>() { /* ... */ }
> void bar<T>() { /* ... */ }
> std::vector<T> baz;
> }
>
> I'm pretty sure somebody has already proposed this - am I right? If so,=
=20
> can someone give me a link to the discussion and/or the decision? GIve a=
=20
> brief explanation of why this was rejected?
>
> It seems to me (correct me if I'm wrong) that this should not interfere=
=20
> with existing syntax not make the compiler have to work harder.
>
>
This seems like a templated namespace. Some pointers on that:
https://groups.google.com/a/isocpp.org/forum/#!searchin/std-proposals/templ=
ate$20namespace/std-proposals/8IDbb1L5_UA/MqmIYyYHVUcJ
https://groups.google.com/a/isocpp.org/forum/#!searchin/std-proposals/templ=
ate$20namespace/std-proposals/ficfBQIgR4c/FRPjJ1QFcjoJ
https://groups.google.com/a/isocpp.org/forum/#!search/cschreib/std-proposal=
s/-3Q3C5FPuPE/L7yaWHAJwmsJ

It seems the idea (which I like) has not generated much enthusiasm so far.=
=20
I suppose this is because you can mostly achieve the same result by using a=
=20
templated structure:
template <typename T>
struct somestruct {
  static void foo() { /* ... */ }
  static void bar() { /* ... */ }
  static std::vector<T> baz;
};

The only difference is that you have to prefix all your calls by=20
"somestruct::", and add "static" to all the declarations. Not the=20
prettiest, but it works.

--=20
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 e=
mail 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/cc8aaf35-d848-477e-b185-ffa260df0265%40isocpp.or=
g.

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

<div dir=3D"ltr">Le samedi 28 janvier 2017 09:26:29 UTC+1, eya...@tx.techni=
on.ac.il a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quote" style=3D"marg=
in: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><d=
iv dir=3D"ltr">(I&#39;ve moved this question from <a href=3D"http://stackov=
erflow.com/questions/41904201/" target=3D"_blank" rel=3D"nofollow" onmoused=
own=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackov=
erflow.com%2Fquestions%2F41904201%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQj=
CNHH5_FW9SFDJRMToqaXR2XS50RVsA&#39;;return true;" onclick=3D"this.href=3D&#=
39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestion=
s%2F41904201%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHH5_FW9SFDJRMToqaXR=
2XS50RVsA&#39;;return true;">http://stackoverflow.com/<wbr>questions/419042=
01/</a> as per suggestions there.)<br><br>In C++, we currently have to temp=
late function and variable definitions individually:<br><br><div style=3D"b=
ackground-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style=
:solid;border-width:1px"><code><div><span style=3D"color:#008">template</sp=
an><span style=3D"color:#000"> </span><span style=3D"color:#660">&lt;</span=
><span style=3D"color:#008">typename</span><span style=3D"color:#000"> T</s=
pan><span style=3D"color:#660">&gt;</span><span style=3D"color:#000"> </spa=
n><span style=3D"color:#008">void</span><span style=3D"color:#000"> foo</sp=
an><span style=3D"color:#660">()</span><span style=3D"color:#000"> </span><=
span style=3D"color:#660">{</span><span style=3D"color:#000"> </span><span =
style=3D"color:#800">/* ... */</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">}</span><span style=3D"color:#000"><br></span><span=
 style=3D"color:#008">template</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">&lt;</span><span style=3D"color:#008">typename</spa=
n><span style=3D"color:#000"> T</span><span style=3D"color:#660">&gt;</span=
><span style=3D"color:#000"> </span><span style=3D"color:#000"><code><span =
style=3D"color:#008">void</span><span style=3D"color:#000"> </span><span st=
yle=3D"color:#000"></span></code>bar</span><span style=3D"color:#660">()</s=
pan><span style=3D"color:#000"> </span><span style=3D"color:#660">{</span><=
span style=3D"color:#000"> </span><span style=3D"color:#800">/* ... */</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#660">}</span><sp=
an style=3D"color:#000"><br></span><span style=3D"color:#008">template</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#660">&lt;</span>=
<span style=3D"color:#008">typename</span><span style=3D"color:#000"> T</sp=
an><span style=3D"color:#660">&gt;</span><span style=3D"color:#000"> std</s=
pan><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</sp=
an><span style=3D"color:#660">&gt;</span><span style=3D"color:#000"> baz</s=
pan><span style=3D"color:#660">;</span><span style=3D"color:#000"><br></spa=
n></div></code></div><br>well, I would expect the template keyword to be ab=
le to apply to braced scopse, similarly to namespaces, so that I could writ=
e:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(=
187,187,187);border-style:solid;border-width:1px"><code><div><span style=3D=
"color:#008">template</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">&lt;</span><span style=3D"color:#008">typename</span><span =
style=3D"color:#000"> T</span><span style=3D"color:#660">&gt;</span><span s=
tyle=3D"color:#000"> </span><span style=3D"color:#660">{</span><span style=
=3D"color:#000"><br></span><span style=3D"color:#000"><code><span style=3D"=
color:#008">void</span><span style=3D"color:#000"> </span><span style=3D"co=
lor:#000"></span></code>foo</span><span style=3D"color:#660">()</span><span=
 style=3D"color:#000"> </span><span style=3D"color:#660">{</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#800">/* ... */</span><span s=
tyle=3D"color:#000"> </span><span style=3D"color:#660">}</span><span style=
=3D"color:#000"><br></span><span style=3D"color:#000"><code><span style=3D"=
color:#008">void</span><span style=3D"color:#000"> </span><span style=3D"co=
lor:#000"></span></code>bar</span><span style=3D"color:#660">()</span><span=
 style=3D"color:#000"> </span><span style=3D"color:#660">{</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#800">/* ... */</span><span s=
tyle=3D"color:#000"> </span><span style=3D"color:#660">}</span><span style=
=3D"color:#000"><br>std</span><span style=3D"color:#660">::</span><span sty=
le=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 st=
yle=3D"color:#000"> baz</span><span style=3D"color:#660">;</span><span styl=
e=3D"color:#000"><br></span><span style=3D"color:#660">}</span><span style=
=3D"color:#000"><br></span></div></code></div><br>or at least<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"><code><div><span style=3D"color:#008">tem=
plate</span><span style=3D"color:#000"> </span><span style=3D"color:#660">&=
lt;</span><span style=3D"color:#008">typename</span><span style=3D"color:#0=
00"> T</span><span style=3D"color:#660">&gt;</span><span style=3D"color:#00=
0"> </span><span style=3D"color:#660">{</span><span style=3D"color:#000"><b=
r></span><span style=3D"color:#000"><code><span style=3D"color:#008">void</=
span><span style=3D"color:#000"> </span><span style=3D"color:#000"></span><=
/code>foo</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"> </span><span style=3D"color:#660">{</span><span style=3D"color:#000"=
> </span><span style=3D"color:#800">/* ... */</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#660">}</span><span style=3D"color:#000"><=
br></span><span style=3D"color:#000"><code><span style=3D"color:#008">void<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#000"></span>=
</code>bar</span><span style=3D"color:#660"><code><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:#660"></span></code>()</span><span style=3D=
"color:#000"> </span><span style=3D"color:#660">{</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#800">/* ... */</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#660">}</span><span style=3D"color:=
#000"><br>std</span><span style=3D"color:#660">::</span><span style=3D"colo=
r:#000">vector</span><span style=3D"color:#660">&lt;</span><span style=3D"c=
olor:#000">T</span><span style=3D"color:#660">&gt;</span><span style=3D"col=
or:#000"> baz</span><span style=3D"color:#660">;</span><span style=3D"color=
:#000"><br></span><span style=3D"color:#660">}</span><span style=3D"color:#=
000"><br></span></div></code></div><br>I&#39;m pretty sure somebody has alr=
eady proposed this - am I right? If so, can someone give me a link to the d=
iscussion and/or the decision? GIve a brief explanation of why this was rej=
ected?<br><br>It seems to me (correct me if I&#39;m wrong) that this should=
 not interfere with existing syntax not make the compiler have to work hard=
er.<br><br></div></blockquote><div><br>This seems like a templated namespac=
e. Some pointers on that:<br>https://groups.google.com/a/isocpp.org/forum/#=
!searchin/std-proposals/template$20namespace/std-proposals/8IDbb1L5_UA/MqmI=
YyYHVUcJ<br>https://groups.google.com/a/isocpp.org/forum/#!searchin/std-pro=
posals/template$20namespace/std-proposals/ficfBQIgR4c/FRPjJ1QFcjoJ<br>https=
://groups.google.com/a/isocpp.org/forum/#!search/cschreib/std-proposals/-3Q=
3C5FPuPE/L7yaWHAJwmsJ<br><br>It seems the idea (which I like) has not gener=
ated much enthusiasm so far. I suppose this is because you can mostly achie=
ve the same result by using a templated structure:<br><div style=3D"backgro=
und-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-sty=
le: solid; border-width: 1px; overflow-wrap: break-word;" class=3D"prettypr=
int"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=
=3D"color: #008;" class=3D"styled-by-prettify">template</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">typename</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"><br></span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">struct</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> somestruct </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=
">static</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">void</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> foo</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: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"sty=
led-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">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r>=C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">s=
tatic</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">void</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> bar</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">()</span><span style=3D"co=
lor: #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"> </span><span style=3D"color: #800;" class=3D"style=
d-by-prettify">/* ... */</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">}</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">sta=
tic</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><span st=
yle=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"> baz</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br></span></div></code></div><br>The only difference is that you have to =
prefix all your calls by &quot;somestruct::&quot;, and  add &quot;static&qu=
ot; to all the declarations. Not the prettiest, but it works.<br></div></di=
v>

<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/cc8aaf35-d848-477e-b185-ffa260df0265%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/cc8aaf35-d848-477e-b185-ffa260df0265=
%40isocpp.org</a>.<br />

------=_Part_985_218977819.1485687681748--

------=_Part_984_194142502.1485687681748--

.