Topic: Use new keyword to indicate that a function should
Author: sunsetburninginmysoul@gmail.com
Date: Sun, 21 Oct 2018 21:43:42 -0700 (PDT)
Raw View
------=_Part_3946_839980232.1540183422411
Content-Type: multipart/alternative;
boundary="----=_Part_3947_457904169.1540183422412"
------=_Part_3947_457904169.1540183422412
Content-Type: text/plain; charset="UTF-8"
We could write a function to take something allocated on the stack and
return a copy of it on the heap:
template<class T> T* as_new(T&& obj) {
return new T{ std::forward<decltype(obj)>(obj)};
}
And this works in most cases while only incurring a performance hit.
However, it breaks down when using objects that either can't be copied or
moved. Providing overloads of functions that allocate something in a
specific provided space can be tedious, too.
*Proposal: *putting the new keyword in parethesis before a function or
expression results in the output of that expression being allocated on the
heap, or in the provided buffer.
Let's take the function make_tuple as an example:
#include <tuple>
int main() {
using std::tuple;
using std::make_tuple;
/* Allocates tuple normally */
tuple<int, double> t1 = make_tuple(4, 0.3);
/*Allocates tuple on heap, returns pointer */
tuple<int, double>* t2 = (new) make_tuple(4, 0.3);
char buff[100];
/* Allocates tuple in buff, returns pointer */
tuple<int, double>* t3 = (new(buff)) make_tuple(4, 0.3);
}
if a function returns a reference, it's a compiler error to use this
construct, and if the function returns a pointer, then this construct
returns a pointer to that pointer.
--
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/269cd5f8-0201-431b-9683-52a945aa9c3d%40isocpp.org.
------=_Part_3947_457904169.1540183422412
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">We could write a function to take something allocated on t=
he stack and return a copy of it on the heap:<div><br></div><div><div class=
=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-colo=
r: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wra=
p: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><=
span style=3D"color: #008;" class=3D"styled-by-prettify">template</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> T</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><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> as_new</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&&=
amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> obj</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><spa=
n 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 =C2=A0 </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">new</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> std</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">::</span><font color=3D"#000000"><span style=3D"color: #000;" class=3D"=
styled-by-prettify">forward</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify"><</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">decltype</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
obj</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)></=
span></font><span style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">obj</span><spa=
n 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"co=
lor: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br></span></div></code></div><div><br></di=
v>And this works in most cases while only incurring a performance hit. Howe=
ver, it breaks down when using objects that either can't be copied or m=
oved. Providing overloads of functions that allocate something in a specifi=
c provided space can be tedious, too.=C2=A0</div><div><br></div><div><b>Pro=
posal: </b>putting the new keyword in parethesis before a function or expre=
ssion results in the output of that expression being allocated on the heap,=
or in the provided buffer.=C2=A0</div><div><br></div><div>Let's take t=
he function make_tuple as an example:<br></div><div><br></div><div><div cla=
ss=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-co=
lor: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-w=
rap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"=
><div style=3D"color: rgb(65, 65, 65); background-color: rgb(251, 251, 251)=
; font-family: "Droid Sans Mono", monospace, monospace, "Dro=
id Sans Fallback"; font-size: 14px; line-height: 19px; white-space: pr=
e;"><div style=3D"font-family: "Droid Sans Mono", monospace, mono=
space, "Droid Sans Fallback"; line-height: 19px;"><div><span styl=
e=3D"color: #05ad97;"><span style=3D"color: #800;" class=3D"styled-by-prett=
ify">#include</span></span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #3d9ca9;"><span style=3D"color: #08=
0;" class=3D"styled-by-prettify"><tuple></span></span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span></div><div><span style=
=3D"color: #d10771;"><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">int</span></span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #8d12ba;"><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">main</span></span><span style=3D"color: #660;" c=
lass=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></div><div><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #05ad97;"><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">using</span></span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">tuple</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">;</span></div><div><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"col=
or: #05ad97;"><span style=3D"color: #008;" class=3D"styled-by-prettify">usi=
ng</span></span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">make_tuple</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span></div><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><div><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify">=C2=A0 =C2=A0 </spa=
n><span style=3D"color: #555555;font-style: italic;"><span style=3D"color: =
#800;" class=3D"styled-by-prettify">/* Allocates tuple normally */</span></=
span></div><div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
=C2=A0 =C2=A0tuple</span><span style=3D"color: #4d4d4d;"><span style=3D"col=
or: #660;" class=3D"styled-by-prettify"><</span></span><span style=3D"co=
lor: #d10771;"><span style=3D"color: #008;" class=3D"styled-by-prettify">in=
t</span></span><span style=3D"color: #660;" class=3D"styled-by-prettify">,<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #d10771;"><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">double</span></span><span style=3D"color: #4d4d4d;"><span style=
=3D"color: #660;" class=3D"styled-by-prettify">></span></span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> t1 </span><span style=3D"c=
olor: #4d4d4d;"><span style=3D"color: #660;" class=3D"styled-by-prettify">=
=3D</span></span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #8d12ba;"><span style=3D"color: #000;" class=
=3D"styled-by-prettify">make_tuple</span></span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">(</span><span style=3D"color: #cc5200;"><spa=
n style=3D"color: #066;" class=3D"styled-by-prettify">4</span></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: #c=
c5200;"><span style=3D"color: #066;" class=3D"styled-by-prettify">0.3</span=
></span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span>=
</div><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span>=
<div><span style=3D"color: #000;" class=3D"styled-by-prettify">=C2=A0 =C2=
=A0 </span><span style=3D"color: #555555;font-style: italic;"><span style=
=3D"color: #800;" class=3D"styled-by-prettify">/*Allocates tuple on heap, r=
eturns pointer */</span></span></div><div><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> =C2=A0 =C2=A0tuple</span><span style=3D"color: #4=
d4d4d;"><span style=3D"color: #660;" class=3D"styled-by-prettify"><</spa=
n></span><span style=3D"color: #d10771;"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">int</span></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: #d10771;"><span style=3D"color=
: #008;" class=3D"styled-by-prettify">double</span></span><span style=3D"co=
lor: #4d4d4d;"><span style=3D"color: #660;" class=3D"styled-by-prettify">&g=
t;*</span></span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
t2 </span><span style=3D"color: #4d4d4d;"><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">=3D</span></span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #05ad97;"><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">new</span></span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #8d12ba;"><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">make_tuple</span></sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #cc5200;"><span style=3D"color: #066;" class=3D"styled-by-p=
rettify">4</span></span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #cc5200;"><span style=3D"color: #066;" class=3D=
"styled-by-prettify">0.3</span></span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> </span></div><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br></span><div><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">=C2=A0 =C2=A0 </span><span style=3D"color: #d10771;"><span style=
=3D"color: #008;" class=3D"styled-by-prettify">char</span></span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> buff</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">[</span><span style=3D"color: #c=
c5200;"><span style=3D"color: #066;" class=3D"styled-by-prettify">100</span=
></span><span style=3D"color: #660;" class=3D"styled-by-prettify">];</span>=
</div><div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=
=A0 =C2=A0</span></div><div><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #555555;font-style: i=
talic;"><span style=3D"color: #800;" class=3D"styled-by-prettify">/* Alloca=
tes tuple in buff, returns pointer */</span></span></div><div><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0tuple</span><s=
pan style=3D"color: #4d4d4d;"><span style=3D"color: #660;" class=3D"styled-=
by-prettify"><</span></span><span style=3D"color: #d10771;"><span style=
=3D"color: #008;" class=3D"styled-by-prettify">int</span></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: #d10771=
;"><span style=3D"color: #008;" class=3D"styled-by-prettify">double</span><=
/span><span style=3D"color: #4d4d4d;"><span style=3D"color: #660;" class=3D=
"styled-by-prettify">>*</span></span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> t3 </span><span style=3D"color: #4d4d4d;"><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span></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: #8d1=
2ba;"><span style=3D"color: #008;" class=3D"styled-by-prettify">new</span><=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">buff</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: #8d1=
2ba;"><span style=3D"color: #000;" class=3D"styled-by-prettify">make_tuple<=
/span></span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</s=
pan><span style=3D"color: #cc5200;"><span style=3D"color: #066;" class=3D"s=
tyled-by-prettify">4</span></span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #cc5200;"><span style=3D"color: #066;=
" class=3D"styled-by-prettify">0.3</span></span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">);</span></div><div><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">}</span></div><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span></div></div></div></code></div><b=
r>if a function returns a reference, it's a compiler error to use this =
construct, and if the function returns a pointer, then this construct retur=
ns a pointer to that pointer.=C2=A0</div><div><br></div><div><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" 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/269cd5f8-0201-431b-9683-52a945aa9c3d%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/269cd5f8-0201-431b-9683-52a945aa9c3d=
%40isocpp.org</a>.<br />
------=_Part_3947_457904169.1540183422412--
------=_Part_3946_839980232.1540183422411--
.