Topic: Lambdas - a helper function for capturing by
Author: Nevin Liber <nevin@eviloverlord.com>
Date: Wed, 11 Jun 2014 11:41:25 -0400
Raw View
--f46d0435c052549e2e04fb9149a5
Content-Type: text/plain; charset=UTF-8
On 11 June 2014 10:23, <fl.lllj@gmail.com> wrote:
> In C++14 we can capture a variable by *const* reference if we cast
> variable to a const reference:
> template<typename T> T const& const_ref(T const& t) { return t; }
>
> auto f = [&a=const_ref(x)]() {
> ++a; // error, `a` is `int const&`
> return a;
> };
>
> But we don't have such `const_ref` function in the standard library, and
> it would be great it we had one.
> We already have another type-casting function called std::move, which
> casts to T&&, and this const_ref function will cast to T const&.
>
I believe this will have the desired semantics:
auto f = [a=std::cref(x)]() {
++a; // error, `a` is `int const&`
return a;
};
> Quite sad that it's too late to put it in C++14, it would work well with
> generalized lambda capture.
>
std::cref is in C++11.
--
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> (847) 691-1404
--
---
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/.
--f46d0435c052549e2e04fb9149a5
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On 11 June 2014 10:23, <span dir=3D"ltr"><<a href=3D"m=
ailto:fl.lllj@gmail.com" target=3D"_blank">fl.lllj@gmail.com</a>></span>=
wrote:<br><div class=3D"gmail_extra"><div class=3D"gmail_quote"><blockquot=
e class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width=
:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-lef=
t:1ex">
<div dir=3D"ltr">In C++14 we can capture a variable by <b>const</b> referen=
ce if we cast variable to a const reference:<br><div style=3D"background-co=
lor:rgb(250,250,250);border:1px solid rgb(187,187,187);word-wrap:break-word=
">
<code><div><span style=3D"color:rgb(0,0,136)">template</span><span style=3D=
"color:rgb(102,102,0)"><</span><span style=3D"color:rgb(0,0,136)">typena=
me</span><span style=3D"color:rgb(0,0,0)"> T</span><span style=3D"color:rgb=
(102,102,0)">></span><span style=3D"color:rgb(0,0,0)"> T </span><span st=
yle=3D"color:rgb(0,0,136)">const</span><span style=3D"color:rgb(102,102,0)"=
>&</span><span style=3D"color:rgb(0,0,0)"> const_ref</span><span style=
=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,0)">T </span=
><span style=3D"color:rgb(0,0,136)">const</span><span style=3D"color:rgb(10=
2,102,0)">&</span><span style=3D"color:rgb(0,0,0)"> t</span><span style=
=3D"color:rgb(102,102,0)">)</span><span style=3D"color:rgb(0,0,0)"> </span>=
<span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0=
)"> </span><span style=3D"color:rgb(0,0,136)">return</span><span style=3D"c=
olor:rgb(0,0,0)"> t</span><span style=3D"color:rgb(102,102,0)">;</span><spa=
n style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">}=
</span><span style=3D"color:rgb(0,0,0)"><br>
<br></span><span style=3D"color:rgb(0,0,136)">auto</span><span style=3D"col=
or:rgb(0,0,0)"> f </span><span style=3D"color:rgb(102,102,0)">=3D</span><sp=
an style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">=
[&</span><span style=3D"color:rgb(0,0,0)">a</span><span style=3D"color:=
rgb(102,102,0)">=3D</span><span style=3D"color:rgb(0,0,0)">const_ref</span>=
<span style=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,0=
)">x</span><span style=3D"color:rgb(102,102,0)">)]()</span><span style=3D"c=
olor:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">{</span><span=
style=3D"color:rgb(0,0,0)"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:rgb(102,102,0)">++</span><span st=
yle=3D"color:rgb(0,0,0)">a</span><span style=3D"color:rgb(102,102,0)">;</sp=
an><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(136,0,=
0)">// error, `a` is `int const&`</span><span style=3D"color:rgb(0,0,0)=
"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">return</span><span =
style=3D"color:rgb(0,0,0)"> a</span><span style=3D"color:rgb(102,102,0)">;<=
/span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(=
102,102,0)">};</span><span style=3D"color:rgb(0,0,0)"><br>
</span></div></code></div><br>But we don't have such `const_ref` functi=
on in the standard library, and it would be great it we had one.<br>We alre=
ady have another type-casting function called std::move, which casts to T&a=
mp;&, and this const_ref function will cast to T const&.<br>
</div></blockquote><div><br></div><div>I believe this will have the desired=
semantics:=C2=A0</div><div><br></div><span style=3D"font-family:monospace;=
background-color:rgb(250,250,250);color:rgb(0,0,136)">auto</span><span styl=
e=3D"font-family:monospace;background-color:rgb(250,250,250);color:rgb(0,0,=
0)">=C2=A0f=C2=A0</span><span style=3D"font-family:monospace;background-col=
or:rgb(250,250,250);color:rgb(102,102,0)">=3D</span><span style=3D"font-fam=
ily:monospace;background-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0</s=
pan><span style=3D"font-family:monospace;background-color:rgb(250,250,250);=
color:rgb(102,102,0)">[</span><span style=3D"font-family:monospace;backgrou=
nd-color:rgb(250,250,250);color:rgb(0,0,0)">a</span><span style=3D"font-fam=
ily:monospace;background-color:rgb(250,250,250);color:rgb(102,102,0)">=3Dst=
d::</span><span style=3D"font-family:monospace;background-color:rgb(250,250=
,250);color:rgb(0,0,0)">cref</span><span style=3D"font-family:monospace;bac=
kground-color:rgb(250,250,250);color:rgb(102,102,0)">(</span><span style=3D=
"font-family:monospace;background-color:rgb(250,250,250);color:rgb(0,0,0)">=
x</span><span style=3D"font-family:monospace;background-color:rgb(250,250,2=
50);color:rgb(102,102,0)">)]()</span><span style=3D"font-family:monospace;b=
ackground-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0</span><span style=
=3D"font-family:monospace;background-color:rgb(250,250,250);color:rgb(102,1=
02,0)">{</span><span style=3D"font-family:monospace;background-color:rgb(25=
0,250,250);color:rgb(0,0,0)"><br>
=C2=A0 =C2=A0=C2=A0</span><span style=3D"font-family:monospace;background-c=
olor:rgb(250,250,250);color:rgb(102,102,0)">++</span><span style=3D"font-fa=
mily:monospace;background-color:rgb(250,250,250);color:rgb(0,0,0)">a</span>=
<span style=3D"font-family:monospace;background-color:rgb(250,250,250);colo=
r:rgb(102,102,0)">;</span><span style=3D"font-family:monospace;background-c=
olor:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0</span><span style=3D"font-fa=
mily:monospace;background-color:rgb(250,250,250);color:rgb(136,0,0)">// err=
or, `a` is `int const&`</span><span style=3D"font-family:monospace;back=
ground-color:rgb(250,250,250);color:rgb(0,0,0)"><br>
=C2=A0 =C2=A0=C2=A0</span><span style=3D"font-family:monospace;background-c=
olor:rgb(250,250,250);color:rgb(0,0,136)">return</span><span style=3D"font-=
family:monospace;background-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0=
a</span><span style=3D"font-family:monospace;background-color:rgb(250,250,2=
50);color:rgb(102,102,0)">;</span><span style=3D"font-family:monospace;back=
ground-color:rgb(250,250,250);color:rgb(0,0,0)"><br>
</span><span style=3D"font-family:monospace;background-color:rgb(250,250,25=
0);color:rgb(102,102,0)">};</span><span style=3D"font-family:monospace;back=
ground-color:rgb(250,250,250);color:rgb(0,0,0)"><br></span><div>=C2=A0<br><=
/div>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr">Quite sad that it's too late to put i=
t in C++14, it would work well with generalized lambda capture.</div>
</blockquote><div><br></div><div>std::cref is in C++11.=C2=A0</div></div>--=
<br>=C2=A0Nevin ":-)" Liber=C2=A0 <mailto:<a href=3D"mailto:n=
evin@eviloverlord.com" target=3D"_blank">nevin@eviloverlord.com</a>>=C2=
=A0 (847) 691-1404
</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 />
--f46d0435c052549e2e04fb9149a5--
.
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@gmail.com>
Date: Wed, 11 Jun 2014 19:36:43 +0200
Raw View
2014-06-11 17:41 GMT+02:00 Nevin Liber <nevin@eviloverlord.com>:
> On 11 June 2014 10:23, <fl.lllj@gmail.com> wrote:
>>
>> In C++14 we can capture a variable by const reference if we cast variable
>> to a const reference:
>> template<typename T> T const& const_ref(T const& t) { return t; }
>>
>> auto f = [&a=const_ref(x)]() {
>> ++a; // error, `a` is `int const&`
>> return a;
>> };
>>
>> But we don't have such `const_ref` function in the standard library, and
>> it would be great it we had one.
>> We already have another type-casting function called std::move, which
>> casts to T&&, and this const_ref function will cast to T const&.
>
> I believe this will have the desired semantics:
>
> auto f = [a=std::cref(x)]() {
>
> ++a; // error, `a` is `int const&`
> return a;
> };
I don't think that it works as *intended*, because cref returns a
reference_wrapper which has no operator++ overload.
- Daniel
--
---
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: Nevin Liber <nevin@eviloverlord.com>
Date: Wed, 11 Jun 2014 13:47:14 -0400
Raw View
--047d7b5d2dea4c29e604fb930be4
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
On 11 June 2014 13:36, Daniel Kr=C3=BCgler <daniel.kruegler@gmail.com> wrot=
e:
> 2014-06-11 17:41 GMT+02:00 Nevin Liber <nevin@eviloverlord.com>:
> > On 11 June 2014 10:23, <fl.lllj@gmail.com> wrote:
> >> template<typename T> T const& const_ref(T const& t) { return t; }
> >>
> >> auto f =3D [&a=3Dconst_ref(x)]() {
> >> ++a; // error, `a` is `int const&`
> >> return a;
> >> };
> >>
> > auto f =3D [a=3Dstd::cref(x)]() {
> >
> > ++a; // error, `a` is `int const&`
> > return a;
> > };
>
> I don't think that it works as *intended*, because cref returns a
> reference_wrapper which has no operator++ overload.
>
Unless I'm reading it wrong, the OP *wants* a compile time error if you try
to increment a. And the error I'm getting from clang is:
*error: **cannot increment value of type*
* 'const std::__1::reference_wrapper<const int>' [Semantic Issue]*
auto f =3D [a =3D std::cref(x)]{ ++a; return a; };
which is the appropriate error. And it compiles and runs just fine if I
change std::cref to std::ref.
--=20
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> (847) 691-1404
--=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/.
--047d7b5d2dea4c29e604fb930be4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On 11 June 2014 13:36, Daniel Kr=C3=BCgler <span dir=3D"lt=
r"><<a href=3D"mailto:daniel.kruegler@gmail.com" target=3D"_blank">danie=
l.kruegler@gmail.com</a>></span> wrote:<br><div class=3D"gmail_extra"><d=
iv class=3D"gmail_quote">
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex">2014-06-11 17:41 GMT+02:00 Nevin Liber <<a href=3D"mail=
to:nevin@eviloverlord.com">nevin@eviloverlord.com</a>>:<br>
<div class=3D"">> On 11 June 2014 10:23, <<a href=3D"mailto:fl.lllj@g=
mail.com">fl.lllj@gmail.com</a>> wrote:<br>>> template<typename=
T> T const& const_ref(T const& t) { return t; }<br>
>><br>
>> auto f =3D [&a=3Dconst_ref(x)]() {<br>
>> =C2=A0 =C2=A0 ++a; // error, `a` is `int const&`<br>
>> =C2=A0 =C2=A0 return a;<br>
>> };<br>
>><br>> auto f =3D [a=3Dstd::cref(x)]() {<br>
><br>
> =C2=A0 =C2=A0 ++a; // error, `a` is `int const&`<br>
> =C2=A0 =C2=A0 return a;<br>
> };<br>
<br>
</div>I don't think that it works as *intended*, because cref returns a=
<br>
reference_wrapper which has no operator++ overload.<br></blockquote><div><b=
r></div><div>Unless I'm reading it wrong, the OP *wants* a compile time=
error if you try to increment a. =C2=A0And the error I'm getting from =
clang is:</div>
<div><br></div><div><p style=3D"margin:0px;font-size:10px;font-family:'=
Source Code Pro';background-color:rgb(254,244,156)"><span style=3D"colo=
r:#bc321c"><b>error: </b></span><b>cannot increment value of type</b></p>
<p style=3D"margin:0px;font-size:10px;font-family:'Source Code Pro'=
;background-color:rgb(254,244,156)"><b>=C2=A0 =C2=A0 =C2=A0 'const std:=
:__1::reference_wrapper<const int>' [Semantic Issue]</b></p>
<p style=3D"margin:0px;font-size:10px;font-family:'Source Code Pro'=
;background-color:rgb(254,244,156)">=C2=A0 =C2=A0 auto f =3D [a =3D std::cr=
ef(x)]{ ++a; return a; };</p></div><div><br></div><div>which is the appropr=
iate error. =C2=A0And it compiles and runs just fine if I change std::cref =
to std::ref.</div>
<div>--=C2=A0<br></div></div>=C2=A0Nevin ":-)" Liber=C2=A0 <ma=
ilto:<a href=3D"mailto:nevin@eviloverlord.com" target=3D"_blank">nevin@evil=
overlord.com</a>>=C2=A0 (847) 691-1404
</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 />
--047d7b5d2dea4c29e604fb930be4--
.
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@gmail.com>
Date: Wed, 11 Jun 2014 20:06:21 +0200
Raw View
2014-06-11 19:47 GMT+02:00 Nevin Liber <nevin@eviloverlord.com>:
> On 11 June 2014 13:36, Daniel Kr=FCgler <daniel.kruegler@gmail.com> wrote=
:
[..]
>> I don't think that it works as *intended*, because cref returns a
>> reference_wrapper which has no operator++ overload.
>
> Unless I'm reading it wrong, the OP *wants* a compile time error if you t=
ry
> to increment a. And the error I'm getting from clang is:
>
> error: cannot increment value of type
>
> 'const std::__1::reference_wrapper<const int>' [Semantic Issue]
>
> auto f =3D [a =3D std::cref(x)]{ ++a; return a; };
>
>
> which is the appropriate error. And it compiles and runs just fine if I
> change std::cref to std::ref.
Well, yes it works in this particular example, but it would not work
for the general case, because the conversion function is only
considered in a restricted set of situations (e.g. not when operator.
is involved). To construct a more realistic example, consider that x
would be a std::vector and you would invoke begin() within the lambda
expression applied to a. Before you respond "use cbegin() here": This
was just an example, I could construct others as well.
- Daniel
--=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: Zhihao Yuan <zy@miator.net>
Date: Wed, 11 Jun 2014 14:33:58 -0400
Raw View
On Wed, Jun 11, 2014 at 1:47 PM, Nevin Liber <nevin@eviloverlord.com> wrote:
>
> auto f = [a = std::cref(x)]{ ++a; return a; };
>
>
> which is the appropriate error. And it compiles and runs just fine if I
> change std::cref to std::ref.
If applying operators of T on std::reference_wrapper<T> is a
recommended use, then we shall never define operators as member
function (while the standard does it everywhere). For example:
char arr[] = "moew";
std::reverse_iterator<char*> x2(arr);
++x2; // good
auto f = [a=std::ref(x2)]
{
++a; // boom!
return a;
};
It only works if reverse_iterator's operator++ is defined as a free
function (for other iterators with unspecified type, implementers can
choose which form to use, then it's a QoI; but things like
reverse_iterator have those hardcoded in synopsis, and here is an
observable difference).
So either we need to fix the standard, or std::reference_wrapper
is not designed to work that way (we don't want user-defined
types to behavior differently from built-in ones).
--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://bit.ly/blog4bsd
--
---
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: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@gmail.com>
Date: Wed, 11 Jun 2014 22:23:02 +0200
Raw View
2014-06-11 20:33 GMT+02:00 Zhihao Yuan <zy@miator.net>:
> If applying operators of T on std::reference_wrapper<T> is a
> recommended use, then we shall never define operators as member
> function (while the standard does it everywhere). For example:
>
> char arr[] = "moew";
> std::reverse_iterator<char*> x2(arr);
> ++x2; // good
> auto f = [a=std::ref(x2)]
> {
> ++a; // boom!
> return a;
> };
>
> It only works if reverse_iterator's operator++ is defined as a free
> function (for other iterators with unspecified type, implementers can
> choose which form to use, then it's a QoI; but things like
> reverse_iterator have those hardcoded in synopsis, and here is an
> observable difference).
>
> So either we need to fix the standard, or std::reference_wrapper
> is not designed to work that way (we don't want user-defined
> types to behavior differently from built-in ones).
std::reference_wrapper isn't designed to work that way. Note also that
for similar reasons (due to being an imperfect proxy), the conceptual
function INVOKE has to be adapted to automatically support
reference_wrapper objects, see
http://cplusplus.github.io/LWG/lwg-active.html#2219
- Daniel
--
---
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: Jim Porter <jvp4846@g.rit.edu>
Date: Wed, 11 Jun 2014 13:33:39 -0700 (PDT)
Raw View
------=_Part_3210_26269455.1402518819561
Content-Type: text/plain; charset=UTF-8
On Wednesday, June 11, 2014 9:23:13 AM UTC-5, Abyx wrote:
>
> In C++14 we can capture a variable by *const* reference if we cast
> variable to a const reference:
> template<typename T> T const& const_ref(T const& t) { return t; }
>
> auto f = [&a=const_ref(x)]() {
> ++a; // error, `a` is `int const&`
> return a;
> };
>
> But we don't have such `const_ref` function in the standard library, and
> it would be great it we had one.
> We already have another type-casting function called std::move, which
> casts to T&&, and this const_ref function will cast to T const&.
>
> Quite sad that it's too late to put it in C++14, it would work well with
> generalized lambda capture.
>
In another discussion[1], a syntax like const(my_object) was suggested,
which has the advantage of working better with rvalue references (see the
discussion for more details on that part).
- Jim
[1]
https://groups.google.com/a/isocpp.org/forum/#!mydiscussions/std-proposals/lWOsBlHAKhI
--
---
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_3210_26269455.1402518819561
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Wednesday, June 11, 2014 9:23:13 AM UTC-5, Abyx wrote:<=
blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bord=
er-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">In C++14 we ca=
n capture a variable by <b>const</b> reference if we cast variable to a con=
st reference:<br><div style=3D"background-color:rgb(250,250,250);border-col=
or:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-wor=
d"><code><div><span style=3D"color:#008">template</span><span style=3D"colo=
r:#660"><</span><span style=3D"color:#008">typename</span><span style=3D=
"color:#000"> T</span><span style=3D"color:#660">></span><span style=3D"=
color:#000"> T </span><span style=3D"color:#008">const</span><span style=3D=
"color:#660">&</span><span style=3D"color:#000"> const_ref</span><span =
style=3D"color:#660">(</span><span style=3D"color:#000">T </span><span styl=
e=3D"color:#008">const</span><span style=3D"color:#660">&</span><span s=
tyle=3D"color:#000"> t</span><span style=3D"color:#660">)</span><span style=
=3D"color:#000"> </span><span style=3D"color:#660">{</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#008">return</span><span style=3D"c=
olor:#000"> t</span><span style=3D"color:#660">;</span><span style=3D"color=
:#000"> </span><span style=3D"color:#660">}</span><span style=3D"color:#000=
"><br><br></span><span style=3D"color:#008">auto</span><span style=3D"color=
:#000"> f </span><span style=3D"color:#660">=3D</span><span style=3D"color:=
#000"> </span><span style=3D"color:#660">[&</span><span style=3D"color:=
#000">a</span><span style=3D"color:#660">=3D</span><span style=3D"color:#00=
0">const_ref</span><span style=3D"color:#660">(</span><span style=3D"color:=
#000">x</span><span style=3D"color:#660">)]()</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:#660">++</span><span style=3D"=
color:#000">a</span><span style=3D"color:#660">;</span><span style=3D"color=
:#000"> </span><span style=3D"color:#800">// error, `a` is `int const&`=
</span><span style=3D"color:#000"><br> </span><span style=3D"c=
olor:#008">return</span><span style=3D"color:#000"> a</span><span style=3D"=
color:#660">;</span><span style=3D"color:#000"><br></span><span style=3D"co=
lor:#660">};</span><span style=3D"color:#000"><br></span></div></code></div=
><br>But we don't have such `const_ref` function in the standard library, a=
nd it would be great it we had one.<br>We already have another type-casting=
function called std::move, which casts to T&&, and this const_ref =
function will cast to T const&.<br><br>Quite sad that it's too late to =
put it in C++14, it would work well with generalized lambda capture.<br></d=
iv></blockquote><div><br>In another discussion[1], a syntax like const(my_o=
bject) was suggested, which has the advantage of working better with rvalue=
references (see the discussion for more details on that part).<br><br>- Ji=
m<br><br>[1] https://groups.google.com/a/isocpp.org/forum/#!mydiscussions/s=
td-proposals/lWOsBlHAKhI <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_3210_26269455.1402518819561--
.
Author: Zhihao Yuan <zy@miator.net>
Date: Wed, 11 Jun 2014 17:00:36 -0400
Raw View
On Wed, Jun 11, 2014 at 4:23 PM, Daniel Kr=C3=BCgler
<daniel.kruegler@gmail.com> wrote:
>>
>> So either we need to fix the standard, or std::reference_wrapper
>> is not designed to work that way (we don't want user-defined
>> types to behavior differently from built-in ones).
>
> std::reference_wrapper isn't designed to work that way. Note also that
> for similar reasons (due to being an imperfect proxy), the conceptual
> function INVOKE has to be adapted to automatically support
> reference_wrapper objects, see
>
> http://cplusplus.github.io/LWG/lwg-active.html#2219
Thanks for referring the issue; I get your point.
--=20
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://bit.ly/blog4bsd
--=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: Zhihao Yuan <zy@miator.net>
Date: Wed, 11 Jun 2014 17:14:27 -0400
Raw View
On Wed, Jun 11, 2014 at 4:33 PM, Jim Porter <jvp4846@g.rit.edu> wrote:
>
> In another discussion[1], a syntax like const(my_object) was suggested,
> which has the advantage of working better with rvalue references (see the
> discussion for more details on that part).
I can tell that you understand what "const reference" is very well, but
then you may know that this solution doesn't work... Because while
`my_object` is a lvalue, const(my_object) is going to be a prvalue
(maybe xvalue, depends on your design), but `&a = ` only binds to
lvalue (same as auto&).
--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://bit.ly/blog4bsd
--
---
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: Jim Porter <jvp4846@g.rit.edu>
Date: Wed, 11 Jun 2014 16:19:48 -0700 (PDT)
Raw View
------=_Part_5_12158432.1402528788397
Content-Type: text/plain; charset=UTF-8
On Wednesday, June 11, 2014 4:14:30 PM UTC-5, Zhihao Yuan wrote:
>
> On Wed, Jun 11, 2014 at 4:33 PM, Jim Porter <jvp...@g.rit.edu
> <javascript:>> wrote:
> >
> > In another discussion[1], a syntax like const(my_object) was suggested,
> > which has the advantage of working better with rvalue references (see
> the
> > discussion for more details on that part).
>
> I can tell that you understand what "const reference" is very well, but
> then you may know that this solution doesn't work... Because while
> `my_object` is a lvalue, const(my_object) is going to be a prvalue
> (maybe xvalue, depends on your design), but `&a = ` only binds to
> lvalue (same as auto&).
>
I'd argue that if const(expr) were added to the language, it should retain
the value category of expr and *only* change the constness. That behavior
is generally what people seem to be looking for here and elsewhere. A
syntax like "const expr" might make that clearer, so that it doesn't look
as much like a prvalue, but I'm not sure the grammar would allow for that.
- Jim
--
---
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_5_12158432.1402528788397
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Wednesday, June 11, 2014 4:14:30 PM UTC-5, Zhihao Yuan =
wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Wed, Jun 11, 2014 at =
4:33 PM, Jim Porter <<a href=3D"javascript:" target=3D"_blank" gdf-obfus=
cated-mailto=3D"hLUeVGs3E44J" onmousedown=3D"this.href=3D'javascript:';retu=
rn true;" onclick=3D"this.href=3D'javascript:';return true;">jvp...@g.rit.e=
du</a>> wrote:
<br>>
<br>> In another discussion[1], a syntax like const(my_object) was sugge=
sted,
<br>> which has the advantage of working better with rvalue references (=
see the
<br>> discussion for more details on that part).
<br>
<br>I can tell that you understand what "const reference" is very well, but
<br>then you may know that this solution doesn't work... Because whil=
e
<br>`my_object` is a lvalue, const(my_object) is going to be a prvalue
<br>(maybe xvalue, depends on your design), but `&a =3D ` only binds to
<br>lvalue (same as auto&).
<br>
</blockquote><div> <br>I'd argue that if const(expr) were added to the=
language, it should retain the value category of expr and *only* change th=
e constness. That behavior is generally what people seem to be looking for =
here and elsewhere. A syntax like "const expr" might make that clearer, so =
that it doesn't look as much like a prvalue, but I'm not sure the grammar w=
ould allow for that.<br><br>- Jim<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_5_12158432.1402528788397--
.
Author: David Krauss <potswa@gmail.com>
Date: Thu, 12 Jun 2014 09:03:01 +0800
Raw View
--Apple-Mail=_36ED88A4-F96F-4C0C-87FB-C23C1E88F906
Content-Type: text/plain; charset=ISO-8859-1
On 2014-06-11, at 10:23 PM, fl.lllj@gmail.com wrote:
> In C++14 we can capture a variable by const reference if we cast variable to a const reference:
> template<typename T> T const& const_ref(T const& t) { return t; }
>
> auto f = [&a=const_ref(x)]() {
> ++a; // error, `a` is `int const&`
> return a;
> };
>
> But we don't have such `const_ref` function in the standard library, and it would be great it we had one.
As others noted, we have std::cref which is imperfect. There is std::cref(x).get() though which should be OK at least for now.
However, I would have expected [const &a = x] to work.
I'm probably very late to this party, but why aren't parameter-declarations allowed as captures? [auto const &a = x] is clear enough. Brevity is nice, but it's not always possible.
--
---
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=_36ED88A4-F96F-4C0C-87FB-C23C1E88F906
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;06–11, at 10:23 PM, <a href=3D"mailto:fl.lllj@gmail.com">fl.lll=
j@gmail.com</a> wrote:</div><br class=3D"Apple-interchange-newline"><blockq=
uote type=3D"cite"><div dir=3D"ltr">In C++14 we can capture a variable by <=
b>const</b> reference if we cast variable to a const reference:<br><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; word-wrap:=
break-word;"><code class=3D"prettyprint"><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">template</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><</span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">typename</span> T<span style=3D"color: #660;" class=3D"st=
yled-by-prettify">></span> T <span style=3D"color: #008;" class=3D"style=
d-by-prettify">const</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">&</span> const_ref<span style=3D"color: #660;" class=3D"style=
d-by-prettify">(</span>T <span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">const</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&</span> t<span style=3D"color: #660;" 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> t<=
span style=3D"color: #660;" class=3D"styled-by-prettify">;</span> <span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">}</span><br><br><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">auto</span> f <span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span> <span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">[&</span>a<span style=3D"color=
: #660;" class=3D"styled-by-prettify">=3D</span>const_ref<span style=3D"col=
or: #660;" class=3D"styled-by-prettify">(</span>x<span style=3D"color: #660=
;" class=3D"styled-by-prettify">)]()</span> <span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><br> <span style=3D"color: =
#660;" class=3D"styled-by-prettify">++</span>a<span style=3D"color: #660;" =
class=3D"styled-by-prettify">;</span> <span style=3D"color: #800;" class=3D=
"styled-by-prettify">// error, `a` is `int const&`</span><br> &nb=
sp; <span style=3D"color: #008;" class=3D"styled-by-prettify">return</span>=
a<span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><br><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">};</span><br></code>=
</div><br>But we don't have such `const_ref` function in the standard libra=
ry, and it would be great it we had one.<br></div></blockquote><div><br></d=
iv><div>As others noted, we have <font face=3D"Courier">std::cref</font> wh=
ich is imperfect. There is <font face=3D"Courier">std::cref(x).get()</font>=
though which should be OK at least for now.</div><div><br></div><div>Howev=
er, I would have expected <font face=3D"Courier">[const &a =3D x]</font=
> to work.</div><div><br></div><div>I’m probably very late to this pa=
rty, but why aren’t parameter-declarations allowed as captures? <font=
face=3D"Courier">[auto const &a =3D x]</font> is clear enough. Br=
evity is nice, but it’s not always possible.</div><div><br></div></di=
v></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" 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=_36ED88A4-F96F-4C0C-87FB-C23C1E88F906--
.