Topic: template lambdas - question


Author: "dgutson ." <danielgutson@gmail.com>
Date: Wed, 26 Aug 2015 17:03:29 -0300
Raw View
Since the following is to be invalid:

template <class T>
struct Lambda
{
    static constexpr auto lambda  =3D []() { T::f(); };
};

and "lambda" cannot be non-constexpr,
is there a way to create a template lambda without having to invent an
(auto) argument to pass the type and do decltype(argument) inside?

Thanks,
  Daniel.

--=20
Who=E2=80=99s got the sweetest disposition?
One guess, that=E2=80=99s who?
Who=E2=80=99d never, ever start an argument?
Who never shows a bit of temperament?
Who's never wrong but always right?
Who'd never dream of starting a fight?
Who get stuck with all the bad luck?

--=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: "dgutson ." <danielgutson@gmail.com>
Date: Wed, 26 Aug 2015 18:21:40 -0300
Raw View
Just to clarify, I posted this question here because I think there's
an opportunity to improve.
I'm working on an axiomatic programming model for C++ and I need a
template-lambda, which I'm assuming don't exist.
Was there any reason?


On Wed, Aug 26, 2015 at 5:03 PM, dgutson . <danielgutson@gmail.com> wrote:
> Since the following is to be invalid:
>
> template <class T>
> struct Lambda
> {
>     static constexpr auto lambda  =3D []() { T::f(); };
> };
>
> and "lambda" cannot be non-constexpr,
> is there a way to create a template lambda without having to invent an
> (auto) argument to pass the type and do decltype(argument) inside?
>
> Thanks,
>   Daniel.
>
> --
> Who=E2=80=99s got the sweetest disposition?
> One guess, that=E2=80=99s who?
> Who=E2=80=99d never, ever start an argument?
> Who never shows a bit of temperament?
> Who's never wrong but always right?
> Who'd never dream of starting a fight?
> Who get stuck with all the bad luck?



--=20
Who=E2=80=99s got the sweetest disposition?
One guess, that=E2=80=99s who?
Who=E2=80=99d never, ever start an argument?
Who never shows a bit of temperament?
Who's never wrong but always right?
Who'd never dream of starting a fight?
Who get stuck with all the bad luck?

--=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: David Krauss <potswa@gmail.com>
Date: Thu, 27 Aug 2015 08:58:56 +0800
Raw View
--Apple-Mail=_5197D76F-8E67-4DD5-9F8E-D011B7F6DE50
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8


> On 2015=E2=80=9308=E2=80=9327, at 4:03 AM, dgutson . <danielgutson@gmail.=
com> wrote:
>=20
> Since the following is to be invalid:
>=20
> template <class T>
> struct Lambda
> {
>    static constexpr auto lambda  =3D []() { T::f(); };
> };

Use a variable template =E2=80=94 they are essentially a shortcut for a cla=
ss with a static member, but auto works better.

template< class T >
auto lambda =3D []() { T::f(); };

> and "lambda" cannot be non-constexpr,

Constexpr lambdas are being studied for C++17, see N4487. Until that=E2=80=
=99s accepted, there=E2=80=99s no combination of these two ingredients at a=
ll.

> is there a way to create a template lambda without having to invent an
> (auto) argument to pass the type and do decltype(argument) inside?

Lambdas are only a shortcut for the more verbose syntax of declaring a clas=
s. Generic lambdas are the only way to obtain a template in a block scope, =
but you=E2=80=99re already in namespace scope.

This generic lambda variable template:

template< class T >
auto ftor =3D [] ( auto && arg ) {
    using A =3D decltype( arg );
    T::f( std::forward< A >( arg ) );
};

is equivalent to this class + variable template:

template< typename T >
struct unlambda {
    template< typename A >
    void operator () ( A && arg ) {
        T::f( std::forward< A >( arg ) );
    }
};

template< typename T >
unlambda< T > ftor;

Suddenly decltype doesn=E2=80=99t seem so inconvenient ;v) . I don=E2=80=99=
t really want to see a syntax for putting template parameter declarations o=
n a lambda expression. It would be a lot of pain for no gain.

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

--Apple-Mail=_5197D76F-8E67-4DD5-9F8E-D011B7F6DE50
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dutf-8"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: s=
pace; -webkit-line-break: after-white-space;" class=3D""><div class=3D""><b=
r class=3D""></div><div class=3D""><div><blockquote type=3D"cite" class=3D"=
"><div class=3D"">On 2015=E2=80=9308=E2=80=9327, at 4:03 AM, dgutson . &lt;=
<a href=3D"mailto:danielgutson@gmail.com" class=3D"">danielgutson@gmail.com=
</a>&gt; wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D"=
">Since the following is to be invalid:<br class=3D""><br class=3D"">templa=
te &lt;class T&gt;<br class=3D"">struct Lambda<br class=3D"">{<br class=3D"=
"> &nbsp;&nbsp;&nbsp;static constexpr auto lambda &nbsp;=3D []() { T::f(); =
};<br class=3D"">};<br class=3D""></div></blockquote><div><br class=3D""></=
div><div class=3D"">Use a variable template =E2=80=94 they are essentially =
a shortcut for a class with a static member, but&nbsp;<font face=3D"Courier=
" class=3D"">auto</font>&nbsp;works better.</div><div class=3D""><br class=
=3D""></div><div class=3D""><font face=3D"Courier" class=3D"">template&lt; =
class T &gt;</font></div><div class=3D""><font face=3D"Courier" class=3D"">=
auto lambda =3D []() { T::f(); };</font></div><div class=3D""><br class=3D"=
"></div><blockquote type=3D"cite" class=3D""><div class=3D"">and "lambda" c=
annot be non-constexpr,<br class=3D""></div></blockquote><div><br class=3D"=
"></div><div class=3D"">Constexpr lambdas are being studied for C++17, see =
N4487. Until that=E2=80=99s accepted, there=E2=80=99s no combination of the=
se two ingredients at all.</div><div class=3D""><br class=3D""></div><block=
quote type=3D"cite" class=3D""><div class=3D"">is there a way to create a t=
emplate lambda without having to invent an<br class=3D"">(auto) argument to=
 pass the type and do decltype(argument) inside?<br class=3D""></div></bloc=
kquote></div><br class=3D""></div><div class=3D"">Lambdas are only a shortc=
ut for the more verbose syntax of declaring a class. Generic lambdas are th=
e only way to obtain a template in a block scope, but you=E2=80=99re alread=
y in namespace scope.</div><div class=3D""><br class=3D""></div><div class=
=3D"">This generic lambda variable template:</div><div class=3D""><br class=
=3D""></div><div class=3D""><div class=3D""><font face=3D"Courier" class=3D=
"">template&lt; class T &gt;</font></div><div class=3D""><font face=3D"Cour=
ier" class=3D"">auto ftor =3D [] ( auto &amp;&amp; arg ) {</font></div><div=
 class=3D""><font face=3D"Courier" class=3D"">&nbsp; &nbsp; using A =3D dec=
ltype( arg );</font></div><div class=3D""><font face=3D"Courier" class=3D""=
>&nbsp; &nbsp; T::f( std::forward&lt; A &gt;( arg ) );</font></div><div cla=
ss=3D""><font face=3D"Courier" class=3D"">};</font></div></div><div class=
=3D""><br class=3D""></div><div class=3D"">is equivalent to this class + va=
riable template:</div><div class=3D""><br class=3D""></div><div class=3D"">=
<font face=3D"Courier" class=3D"">template&lt; typename T &gt;</font></div>=
<div class=3D""><font face=3D"Courier" class=3D"">struct unlambda {</font><=
/div><div class=3D""><font face=3D"Courier" class=3D"">&nbsp; &nbsp; templa=
te&lt; typename A &gt;</font></div><div class=3D""><font face=3D"Courier" c=
lass=3D"">&nbsp; &nbsp; void operator () ( A &amp;&amp; arg ) {</font></div=
><div class=3D""><font face=3D"Courier" class=3D"">&nbsp; &nbsp; &nbsp; &nb=
sp; T</font><span style=3D"font-family: Courier;" class=3D"">::f( std::forw=
ard&lt; A &gt;( arg ) );</span></div><div class=3D""><span style=3D"font-fa=
mily: Courier;" class=3D"">&nbsp; &nbsp; }</span></div><div class=3D""><spa=
n style=3D"font-family: Courier;" class=3D"">};</span></div><div class=3D""=
><span style=3D"font-family: Courier;" class=3D""><br class=3D""></span></d=
iv><div class=3D""><span style=3D"font-family: Courier;" class=3D"">templat=
e&lt; typename T &gt;</span></div><div class=3D""><span style=3D"font-famil=
y: Courier;" class=3D"">unlambda&lt; T &gt; ftor;</span></div><div class=3D=
""><span style=3D"font-family: Courier;" class=3D""><br class=3D""></span><=
/div><div class=3D"">Suddenly <font face=3D"Courier" class=3D"">decltype</f=
ont> doesn=E2=80=99t seem so inconvenient ;v) . I don=E2=80=99t really want=
 to see a syntax for putting template parameter declarations on a lambda ex=
pression. It would be a lot of pain for no gain.</div><div class=3D""><br c=
lass=3D""></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=_5197D76F-8E67-4DD5-9F8E-D011B7F6DE50--

.