Topic: Functions whose body is an expression
Author: "'Johannes Schaub' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 25 Oct 2015 15:42:47 +0100
Raw View
Hello all, my proposal is to allow an initializer (expression) as function body
int abs(int x) = x > 0 ? x : -x;
Or, (with support of SFINAE and overloadability!)
template<typename T> auto size(T &&t) = t.sizte();
template<typename T> auto size(T &&t) = t.count();
template<typename T> auto size(T &&t) = t.length();
What do you think? This would just be syntactic sugar for usual
function template + decltype, but would avoid repetitions by the
programmer and syntactically looks like a useful extension to variable
templates.
--
---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Sun, 25 Oct 2015 16:45:01 +0100
Raw View
--f46d0442718e4faad60522efbaed
Content-Type: text/plain; charset=UTF-8
https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/fEOMohkeQGQ/n1x0eWCEjUMJ
On Sun, Oct 25, 2015 at 3:42 PM, 'Johannes Schaub' via ISO C++ Standard -
Future Proposals <std-proposals@isocpp.org> wrote:
> Hello all, my proposal is to allow an initializer (expression) as function
> body
>
> int abs(int x) = x > 0 ? x : -x;
>
> Or, (with support of SFINAE and overloadability!)
>
> template<typename T> auto size(T &&t) = t.sizte();
> template<typename T> auto size(T &&t) = t.count();
> template<typename T> auto size(T &&t) = t.length();
>
> What do you think? This would just be syntactic sugar for usual
> function template + decltype, but would avoid repetitions by the
> programmer and syntactically looks like a useful extension to variable
> templates.
>
> --
>
> ---
> 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.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
--
---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--f46d0442718e4faad60522efbaed
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><a href=3D"https://groups.google.com/a/isocpp.org/forum/#!=
msg/std-proposals/fEOMohkeQGQ/n1x0eWCEjUMJ">https://groups.google.com/a/iso=
cpp.org/forum/#!msg/std-proposals/fEOMohkeQGQ/n1x0eWCEjUMJ</a><br></div><di=
v class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Sun, Oct 25, 2015=
at 3:42 PM, 'Johannes Schaub' via ISO C++ Standard - Future Propos=
als <span dir=3D"ltr"><<a href=3D"mailto:std-proposals@isocpp.org" targe=
t=3D"_blank">std-proposals@isocpp.org</a>></span> wrote:<br><blockquote =
class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid=
;padding-left:1ex">Hello all, my proposal is to allow an initializer (expre=
ssion) as function body<br>
<br>
=C2=A0 =C2=A0int abs(int x) =3D x > 0 ? x : -x;<br>
<br>
Or, (with support of SFINAE and overloadability!)<br>
<br>
=C2=A0 =C2=A0 template<typename T> auto size(T &&t)=C2=A0 =3D=
t.sizte();<br>
=C2=A0 =C2=A0 template<typename T> auto size(T &&t)=C2=A0 =3D=
t.count();<br>
=C2=A0 =C2=A0 template<typename T> auto size(T &&t)=C2=A0 =3D=
t.length();<br>
<br>
What do you think? This would just be syntactic sugar for usual<br>
function template + decltype, but would avoid repetitions by the<br>
programmer and syntactically looks like a useful extension to variable<br>
templates.<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
--<br>
<br>
---<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%2Bunsubscribe@isocpp.org">std-propo=
sals+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>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" rel=3D"noreferrer" target=3D"_blank">http://groups.google.c=
om/a/isocpp.org/group/std-proposals/</a>.<br>
</font></span></blockquote></div><br></div>
<p></p>
-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--f46d0442718e4faad60522efbaed--
.
Author: "'Johannes Schaub' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 25 Oct 2015 16:52:44 +0100
Raw View
Nice, I had no idea! Has it been officially proposed with a paper? My
main motivation would be to make
http://stackoverflow.com/q/33330211/34509 work, so the literal
replacement by "{ return expr; }" will not work for my proposal.
2015-10-25 16:45 GMT+01:00 Andrew Tomazos <andrewtomazos@gmail.com>:
> https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/fEOMohkeQGQ/n1x0eWCEjUMJ
>
> On Sun, Oct 25, 2015 at 3:42 PM, 'Johannes Schaub' via ISO C++ Standard -
> Future Proposals <std-proposals@isocpp.org> wrote:
>>
>> Hello all, my proposal is to allow an initializer (expression) as function
>> body
>>
>> int abs(int x) = x > 0 ? x : -x;
>>
>> Or, (with support of SFINAE and overloadability!)
>>
>> template<typename T> auto size(T &&t) = t.sizte();
>> template<typename T> auto size(T &&t) = t.count();
>> template<typename T> auto size(T &&t) = t.length();
>>
>> What do you think? This would just be syntactic sugar for usual
>> function template + decltype, but would avoid repetitions by the
>> programmer and syntactically looks like a useful extension to variable
>> templates.
>>
--
---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 25 Oct 2015 10:32:10 -0700 (PDT)
Raw View
------=_Part_4104_1173431866.1445794330432
Content-Type: multipart/alternative;
boundary="----=_Part_4105_1426081624.1445794330432"
------=_Part_4105_1426081624.1445794330432
Content-Type: text/plain; charset=UTF-8
On Sunday, October 25, 2015 at 10:42:50 AM UTC-4, Johannes Schaub wrote:
>
> Hello all, my proposal is to allow an initializer (expression) as function
> body
>
> int abs(int x) = x > 0 ? x : -x;
>
> Or, (with support of SFINAE and overloadability!)
>
> template<typename T> auto size(T &&t) = t.sizte();
> template<typename T> auto size(T &&t) = t.count();
> template<typename T> auto size(T &&t) = t.length();
>
First, that's not overloading, since SFINAE makes the other declarations
disappear. And if it doesn't, you get a straight-up compilation error. That
is, you can't pass a `T` that has both `size` and `count`.
Second, this would be better done with concepts, since the concept would
make it much more obvious what's going on:
auto size(HasSize &&t) {return t.size();}
auto size(HasCount &&t) {return t.count();}
auto size(HasLength &&t) {return t.length();}
Also, such concepts would be a lot more specific. Rather than checking a
single member function or something, they'd be based on things like
`Container` or whatever.
Third, we should not be encouraging this to begin with. If for nothing more
than the reason I cited before: this breaks for types that have both
functions. Instead, the *user* should be providing a `size` overload, with
a *single* default case.
--
---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_4105_1426081624.1445794330432
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>On Sunday, October 25, 2015 at 10:42:50 AM UTC-4, =
Johannes Schaub wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;=
margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Hello al=
l, my proposal is to allow an initializer (expression) as function body
<br>
<br>=C2=A0 =C2=A0int abs(int x) =3D x > 0 ? x : -x;
<br>
<br>Or, (with support of SFINAE and overloadability!)
<br>
<br>=C2=A0 =C2=A0 template<typename T> auto size(T &&t) =C2=
=A0=3D t.sizte();
<br>=C2=A0 =C2=A0 template<typename T> auto size(T &&t) =C2=
=A0=3D t.count();
<br>=C2=A0 =C2=A0 template<typename T> auto size(T &&t) =C2=
=A0=3D t.length();
<br></blockquote><div><br>First, that's not overloading, since SFINAE m=
akes the other declarations disappear. And if it doesn't, you get a str=
aight-up compilation error. That is, you can't pass a `T` that has both=
`size` and `count`.<br><br>Second, this would be better done with concepts=
, since the concept would make it much more obvious what's going on:<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: #008;" class=3D"styled-by-prettify">auto</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> size</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify">HasSize</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">t</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">{</span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">return</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">size</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">();}</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> size</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span><span style=3D"color: #606;" class=3D"st=
yled-by-prettify">HasCount</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-pre=
ttify">t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">return</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;" cla=
ss=3D"styled-by-prettify">count</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: #008;" class=3D"styled-by-p=
rettify">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> size</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(<=
/span><span style=3D"color: #606;" class=3D"styled-by-prettify">HasLength</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">&&</span><spa=
n 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"> </span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">{</span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> t</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">l=
ength</span><span style=3D"color: #660;" class=3D"styled-by-prettify">();}<=
/span></div></code></div><br>Also, such concepts would be a lot more specif=
ic. Rather than checking a single member function or something, they'd =
be based on things like `Container` or whatever.<br><br>Third, we should no=
t be encouraging this to begin with. If for nothing more than the reason I =
cited before: this breaks for types that have both functions. Instead, the =
<i>user</i> should be providing a `size` overload, with a <i>single</i> def=
ault case.<br></div></div>
<p></p>
-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_4105_1426081624.1445794330432--
------=_Part_4104_1173431866.1445794330432--
.
Author: "'Johannes Schaub' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 25 Oct 2015 21:03:13 +0100
Raw View
2015-10-25 18:32 GMT+01:00 Nicol Bolas <jmckesson@gmail.com>:
>
>
> On Sunday, October 25, 2015 at 10:42:50 AM UTC-4, Johannes Schaub wrote:
>>
>> Hello all, my proposal is to allow an initializer (expression) as function
>> body
>>
>> int abs(int x) = x > 0 ? x : -x;
>>
>> Or, (with support of SFINAE and overloadability!)
>>
>> template<typename T> auto size(T &&t) = t.sizte();
>> template<typename T> auto size(T &&t) = t.count();
>> template<typename T> auto size(T &&t) = t.length();
>
>
> First, that's not overloading, since SFINAE makes the other declarations
> disappear. And if it doesn't, you get a straight-up compilation error. That
> is, you can't pass a `T` that has both `size` and `count`.
>
Well, this is overloading. But not overload resolution (to be
hairsplitting, it's like 5% overload resolution because a candidate
set is still formed..).
> Second, this would be better done with concepts, since the concept would
> make it much more obvious what's going on:
>
> auto size(HasSize &&t) {return t.size();}
> auto size(HasCount &&t) {return t.count();}
> auto size(HasLength &&t) {return t.length();}
>
> Also, such concepts would be a lot more specific. Rather than checking a
> single member function or something, they'd be based on things like
> `Container` or whatever.
>
This is a good point. Sufficient to make me forget about this. Thanks!
--
---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.