Topic: should lambda to function pointer conversion


Author: David Krauss <potswa@gmail.com>
Date: Fri, 18 Jul 2014 06:40:20 +0800
Raw View
--Apple-Mail=_24F21A74-A45F-4BDF-BE36-89FA42F03407
Content-Type: text/plain; charset=ISO-8859-1


On 2014-07-17, at 8:46 AM, Adam Nevraumont <afn@theorem.ca> wrote:

> Yes, it should.  This would also allow lambdas to be used as function argument template parameters somewhat.
>
> However nothing in C++ stops that array from being compile time initialized even if it is not constexpr.

I agree with the proposal, but

1. Lambda types can be used as template arguments, including lambdas used as arguments to function templates. However, the body of a lambda is never constexpr-qualified, so you won't be able to parameterize a constexpr higher-order function over lambdas.

2. You never said anything about an array. It sounds like you intended to provide an example,

typedef void (*op_ptr)();
constexpr op_ptr op_table[] = {
 [] { std::cout << "hello"; }, // opcode::hello
 [] { std::cout << "bye"; }, // opcode::goodbye
 [] { done_flag = true; } // opcode::done
};

This looks perfectly reasonable.

--

---
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/.

--Apple-Mail=_24F21A74-A45F-4BDF-BE36-89FA42F03407
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;07&ndash;17, at 8:46 AM, Adam Nevraumont &lt;<a href=3D"mailto:afn@th=
eorem.ca">afn@theorem.ca</a>&gt; wrote:</div><br class=3D"Apple-interchange=
-newline"><blockquote type=3D"cite">Yes, it should. &nbsp;This would also a=
llow lambdas to be used as function argument template parameters somewhat.<=
br><br>However nothing in C++ stops that array from being compile time init=
ialized even if it is not constexpr.<br></blockquote><div><br></div></div>I=
 agree with the proposal, but<div><br></div><div>1. Lambda types can be use=
d as template arguments, including lambdas used as arguments to function te=
mplates. However, the body of a lambda is never constexpr-qualified, so you=
 won&rsquo;t be able to parameterize a constexpr higher-order function over=
 lambdas.</div><div><br></div><div>2. You never said anything about an arra=
y. It sounds like you intended to provide an example,</div><div><br></div><=
div><font face=3D"Courier">typedef void (*op_ptr)();</font></div><div><font=
 face=3D"Courier">constexpr op_ptr op_table[] =3D {</font></div><div><font =
face=3D"Courier"><span class=3D"Apple-tab-span" style=3D"white-space:pre"> =
</span>[] { std::cout &lt;&lt; "hello"; }, // opcode::hello</font></div><di=
v><font face=3D"Courier"><span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre"> </span>[] { std::cout &lt;&lt; "bye"; }, // opcode::goodbye</font><=
/div><div><font face=3D"Courier"><span class=3D"Apple-tab-span" style=3D"wh=
ite-space:pre"> </span>[] { done_flag =3D true; } // opcode::done</font></d=
iv><div><font face=3D"Courier">};&nbsp;</font></div><div><br></div><div>Thi=
s looks perfectly reasonable.</div><div><br></div></body></html>

<p></p>

-- <br />
<br />
--- <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 />
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 />

--Apple-Mail=_24F21A74-A45F-4BDF-BE36-89FA42F03407--

.


Author: Richard Smith <richard@metafoo.co.uk>
Date: Thu, 17 Jul 2014 15:47:20 -0700
Raw View
--001a11c1e2ea6ddb7d04fe6b6cc1
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Thu, Jul 17, 2014 at 3:40 PM, David Krauss <potswa@gmail.com> wrote:

>
> On 2014=E2=80=9307=E2=80=9317, at 8:46 AM, Adam Nevraumont <afn@theorem.c=
a> wrote:
>
> Yes, it should.  This would also allow lambdas to be used as function
> argument template parameters somewhat.
>
> However nothing in C++ stops that array from being compile time
> initialized even if it is not constexpr.
>
>
> I agree with the proposal, but
>
> 1. Lambda types can be used as template arguments, including lambdas used
> as arguments to function templates. However, the body of a lambda is neve=
r
> constexpr-qualified, so you won=E2=80=99t be able to parameterize a const=
expr
> higher-order function over lambdas.
>
> 2. You never said anything about an array. It sounds like you intended to
> provide an example,
>
> typedef void (*op_ptr)();
> constexpr op_ptr op_table[] =3D {
> [] { std::cout << "hello"; }, // opcode::hello
> [] { std::cout << "bye"; }, // opcode::goodbye
> [] { done_flag =3D true; } // opcode::done
> };
>
> This looks perfectly reasonable.
>

+1, there doesn't seem to be any good reason not to allow this.

--=20

---=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

--001a11c1e2ea6ddb7d04fe6b6cc1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On T=
hu, Jul 17, 2014 at 3:40 PM, David Krauss <span dir=3D"ltr">&lt;<a href=3D"=
mailto:potswa@gmail.com" target=3D"_blank">potswa@gmail.com</a>&gt;</span> =
wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div style=3D"word-wrap:break-word"><div cla=
ss=3D""><br><div><div>On 2014=E2=80=9307=E2=80=9317, at 8:46 AM, Adam Nevra=
umont &lt;<a href=3D"mailto:afn@theorem.ca" target=3D"_blank">afn@theorem.c=
a</a>&gt; wrote:</div>
<br><blockquote type=3D"cite">Yes, it should. =C2=A0This would also allow l=
ambdas to be used as function argument template parameters somewhat.<br><br=
>However nothing in C++ stops that array from being compile time initialize=
d even if it is not constexpr.<br>
</blockquote><div><br></div></div></div>I agree with the proposal, but<div>=
<br></div><div>1. Lambda types can be used as template arguments, including=
 lambdas used as arguments to function templates. However, the body of a la=
mbda is never constexpr-qualified, so you won=E2=80=99t be able to paramete=
rize a constexpr higher-order function over lambdas.</div>
<div><br></div><div>2. You never said anything about an array. It sounds li=
ke you intended to provide an example,</div><div><br></div><div><font face=
=3D"Courier">typedef void (*op_ptr)();</font></div><div><font face=3D"Couri=
er">constexpr op_ptr op_table[] =3D {</font></div>
<div><font face=3D"Courier"><span style=3D"white-space:pre-wrap"> </span>[]=
 { std::cout &lt;&lt; &quot;hello&quot;; }, // opcode::hello</font></div><d=
iv><font face=3D"Courier"><span style=3D"white-space:pre-wrap"> </span>[] {=
 std::cout &lt;&lt; &quot;bye&quot;; }, // opcode::goodbye</font></div>
<div><font face=3D"Courier"><span style=3D"white-space:pre-wrap"> </span>[]=
 { done_flag =3D true; } // opcode::done</font></div><div><font face=3D"Cou=
rier">};=C2=A0</font></div><div><br></div><div>This looks perfectly reasona=
ble.</div>
</div></blockquote><div><br></div><div>+1, there doesn&#39;t seem to be any=
 good reason not to allow this.</div></div></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&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 />
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 />

--001a11c1e2ea6ddb7d04fe6b6cc1--

.


Author: Gabriel Dos Reis <gdr@axiomatics.org>
Date: Thu, 17 Jul 2014 17:28:13 -0700
Raw View
frrrwww@gmail.com writes:

| Hello,
|=20
| I sometimes use the following pattern:
|=20
| static const struct {
| =C2=A0 =C2=A0 const char* name;
| =C2=A0 =C2=A0 void (*func)(int);
| } functions[] =3D {
| =C2=A0 =C2=A0 =C2=A0{ "function1", [](int) { ... } },
| =C2=A0 =C2=A0 =C2=A0{ "function2", [](int) { ... } },
| =C2=A0 =C2=A0 =C2=A0...
| };
|=20
| And when I look at the generated code, I see that this array gets
| generated at runtime.
|=20
| Ideally I would like to declare this array constexpr, and that would
| work if I used named functions, however it does not for lambda, and I
| cannot see any good reason for that.
|=20
| As the function pointer conversion operator only exists for state-less
| lambda, would'nt it make sense to define them as constexpr and their
| conversion operator as well ?

It does.

It is sad that we needed to introduce a keyword to get the notion
accepted by the C++ community, and we couldn't just infer
'constexprness' from the (inline) definition and use context.

-- Gaby

--=20

---=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

.


Author: Thiago Macieira <thiago@macieira.org>
Date: Thu, 17 Jul 2014 17:35:51 -0700
Raw View
On Thursday 17 July 2014 17:28:13 Gabriel Dos Reis wrote:
> It is sad that we needed to introduce a keyword to get the notion
> accepted by the C++ community, and we couldn't just infer
> 'constexprness' from the (inline) definition and use context.

I would like to see constexprness inferred from context, but I also appreciate
having the keyword that tells me when I failed to make it constexpr.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

--

---
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: Gabriel Dos Reis <gdr@axiomatics.org>
Date: Thu, 17 Jul 2014 17:52:31 -0700
Raw View
Thiago Macieira <thiago@macieira.org> writes:

| On Thursday 17 July 2014 17:28:13 Gabriel Dos Reis wrote:
| > It is sad that we needed to introduce a keyword to get the notion
| > accepted by the C++ community, and we couldn't just infer
| > 'constexprness' from the (inline) definition and use context.
|
| I would like to see constexprness inferred from context, but I also appreciate
| having the keyword that tells me when I failed to make it constexpr.

I see a distinction between mandatory constexpr and optional constexpr.

As we have been warming up to more compile-time computations, I think it
is becoming clearer and clearer that requiring constexpr is making not
only the code more verbose, but also that failure to repeat
syntactically the semantics information already in possession of the
compiler is making it harder to smoothly develop new programming
techniques and pattern around the language features and standard library
facilities.

-- Gaby

--

---
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/.

.