tag:blogger.com,1999:blog-13960885.post2363799032265032024..comments2024-03-26T02:03:01.072-07:00Comments on C++ Truths: Fun with Lambdas: C++14 Style (part 1)Sumanthttp://www.blogger.com/profile/11957855386259543653noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-13960885.post-35301994444616947442017-06-24T09:52:36.187-07:002017-06-24T09:52:36.187-07:00you stated: "C++ closures do not extend the l...you stated: "C++ closures do not extend the lifetime of their context. (If you need this use shared_ptr)" could you give <br />and example?<br /><a href="https://www.gesmotos.com" rel="nofollow">gclub</a>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-13960885.post-84073262326477975392016-11-27T10:14:44.494-08:002016-11-27T10:14:44.494-08:00The point is that c++ lambdas are *not* closures b...The point is that c++ lambdas are *not* closures because they just honor the external lifetime of the object without making sure the object survives as long as the lambda does. In c++ one way to lengthen an object's lifetime is to make it a shared pointer, which is reference counted. mjohn5https://www.blogger.com/profile/02839200771991653041noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-60779661541148228602016-11-27T08:33:57.776-08:002016-11-27T08:33:57.776-08:00you stated: "C++ closures do not extend the l...you stated: "C++ closures do not extend the lifetime of their context. (If you need this use shared_ptr)" could you give <br />and example?<br />black_13https://www.blogger.com/profile/16676086803660525592noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-72831894356835529502016-07-27T22:29:37.366-07:002016-07-27T22:29:37.366-07:00Yup, I missed that, admittedly, not-so-subtle diff...Yup, I missed that, admittedly, not-so-subtle difference :)Anonymoushttps://www.blogger.com/profile/13015316305424929232noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-25809330450100648232016-07-27T14:28:16.699-07:002016-07-27T14:28:16.699-07:00@Dave Oh yeah, when you have a named inner class i...@Dave Oh yeah, when you have a named inner class inside a lambda, then what you are saying makes sense. However, if the lambda returns a closure then there are few limitations.Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-3186952070774107932016-07-27T11:41:11.749-07:002016-07-27T11:41:11.749-07:00@Sumant
I am quite sure about the rest: http://id...@Sumant<br /><br />I am quite sure about the rest: http://ideone.com/h2ZRs3 :) I think, maybe, that I was confusing something in your example contra mine.<br /><br />/DaveAnonymoushttps://www.blogger.com/profile/13015316305424929232noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-89029859868167225372016-07-22T10:47:40.237-07:002016-07-22T10:47:40.237-07:00@dave Aliasing the type of lambda is fine but are ...@dave Aliasing the type of lambda is fine but are you sure about the rest? I get compiler error for "Foo bar(10)" and is_constructible returns false. Besides, there are many limitations like missing copy-assignment operator for lambdas. class Inner, however it copy-assignable.<br /><br />Live code: http://melpon.org/wandbox/permlink/mfteteOhqXrZGxVS<br /><br /><br /><br />Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-80716730325704440432016-07-22T07:15:40.231-07:002016-07-22T07:15:40.231-07:00Great read; thanks for posting!
Jumping on the ba...Great read; thanks for posting!<br /><br />Jumping on the back of @joaof's comment: Using decltype to ascertain the type of auto x allows for using an interesting pattern of, what the D Language community calls, Voldemort types. Walter Bright wrote an interesting article about it which you can read <a href="http://www.drdobbs.com/cpp/voldemort-types-in-d/232901591?pgno=2" rel="nofollow">here</a>.<br /><br />Interesting to note that, unlike in D, we can later retrieve the type of Inner outside of the lambda:<br /><br />auto foo = identifyf(5);<br />foo(); //5<br />using Foo = decltype(foo);<br />Foo bar(10);<br />bar(); //10Anonymoushttps://www.blogger.com/profile/13015316305424929232noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-43226567213143337982015-12-07T11:21:25.845-08:002015-12-07T11:21:25.845-08:00@mjohn5 No good reason! Just a reminder that good ...@mjohn5 No good reason! Just a reminder that good old functions are equally good candidates. Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-65560265957584689092015-12-07T08:07:31.826-08:002015-12-07T08:07:31.826-08:00Why are add and sub auto, but mult explicitly uses...Why are add and sub auto, but mult explicitly uses int?mjohn5https://www.blogger.com/profile/02839200771991653041noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-23174601236093298542014-09-20T02:28:20.885-07:002014-09-20T02:28:20.885-07:00you can make the first identityf more like the ide...you can make the first identityf more like the identityf lamba:<br /><br /> auto identityf = [](auto x) <br /> {<br /> class inner<br /> {<br /> public:<br /> using X = decltype(x);<br /> X mx;<br /> inner(X x): mx(x) {}<br /> X operator()() { return mx; }<br /> };<br /> return inner{x};<br /> };<br /><br />(you version of inner uses int)<br />joaofnoreply@blogger.comtag:blogger.com,1999:blog-13960885.post-25942677932553181322014-05-22T11:05:22.164-07:002014-05-22T11:05:22.164-07:00@Michael: I agree. The text was confusing and some...@Michael: I agree. The text was confusing and somewhat incorrect. I've taken the liberty to use your words in the updated post.Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-17684980050180507542014-05-13T07:58:24.644-07:002014-05-13T07:58:24.644-07:00Interesting, thank you!
The Lambda != Closure sec...Interesting, thank you!<br /><br />The Lambda != Closure section is confusing though.<br /><br />"A lambda is just an anonymous function." -- I understand the desire to make the explanation as simple as possible, but here it happens at the cost of correctness. "Just functions" don't carry state. Perhaps "A lambda is just a syntax sugar to define anonymous functions and function objects"?<br /><br />"Not all closures are lambdas and not all lambdas are closures." -- sounds weird. A lambda is a syntactic construct (expression), and a closure is a run-time object, an instance of a closure type. Neither can "be" the other.Michaelhttps://www.blogger.com/profile/06246807934535676382noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-71615705916540251702014-03-14T04:35:39.333-07:002014-03-14T04:35:39.333-07:00@Sumant, I enjoyed this post, as I enjoy most of y...@Sumant, I enjoyed this post, as I enjoy most of your posts. Thanks for sharing!Stuarthttps://www.blogger.com/profile/02207284860315741534noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-75760739987636922212014-03-13T22:42:11.367-07:002014-03-13T22:42:11.367-07:00@Jagan. fromto returns a range lambda that returns...@Jagan. fromto returns a range lambda that returns numbers from 0 to 9 one at a time. I've updated the code to reflect that.<br /><br />@Igor: Are you referring to identityf::Inner? For simplicity, I'm using just an int member.Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-44283222061416394272014-03-13T17:29:01.080-07:002014-03-13T17:29:01.080-07:00Please correct identityf. It should use templated ...Please correct identityf. It should use templated class according to description.Igor Mikushkinhttps://www.blogger.com/profile/10415943317265697903noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-8056672004674276532014-03-13T15:54:52.791-07:002014-03-13T15:54:52.791-07:00This comment has been removed by the author.Jaganhttps://www.blogger.com/profile/06960755230152303336noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-78768400326162628882014-03-13T15:54:26.018-07:002014-03-13T15:54:26.018-07:00I am unable to figure out how to get the values in...I am unable to figure out how to get the values in range. I mean how to execute it to get the values in range ?<br /><br />fromto(0, 10); would return the lambda and from(0, 10)(); will only print 0. <br />Isn't that the case ?Jaganhttps://www.blogger.com/profile/06960755230152303336noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-61804606911691751122014-03-13T14:43:46.723-07:002014-03-13T14:43:46.723-07:00@HiroshimaCC: Good point. There was a oversight be...@HiroshimaCC: Good point. There was a oversight because "5" and 5 look the same when printed to stdout. The code now updated. Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-53729221900273868612014-03-13T10:20:21.780-07:002014-03-13T10:20:21.780-07:00Is the Right Identity supposed to be true?Is the Right Identity supposed to be true?HiroshimaCChttps://www.blogger.com/profile/07050218486323791979noreply@blogger.com