I don't get c++11.

Started by
34 comments, last by tanzanite7 10 years, 1 month ago

Lambdas can only be defined and used inside functions:

O_o, wut? Lambda is a closure object - saying that it can only be defined / used inside functions makes as much sense than saying one can use/define the value 7 ONLY inside functions. Do not get confused that the lambda object is called "closure object" - that too is still just an object. In short, there is no such limitation.

Perhaps you meant that defining a capturing Lambda outside functions is conceptually kinda totally bonkers -> capture list requires function scope.

Good catch! I mispoke. I meant that regular functions can only be used outside functions, but Lambdas can also be used inside functions. I think I meant to type "also" but typed "only" instead. ohmy.png

My favorite lambda feature is actually just using them directly as function arguments - which I completely neglected to mention. mellow.png

Lambda object defines a few operators. One that seemed to cause a bit unfortunate/confusing wordings here is: type conversion operator to the underlying function type (only defined if the lambda does not capture anything - as otherwise the operator would not make any sense). IE. Lambda object is NOT just a function, however it might be possible to get a function pointer out of one via implicit/explicit conversion:

I didn't realize non-capturing lambdas didn't just compile into regular functions. Thanks for that!

Advertisement
I didn't realize non-capturing lambdas didn't just compile into regular functions.

It was a late addition to the standard for this conversion operator to even exist at all - so late in fact that VC++ 2012 original release did not support it. Had to use template magic to produce a regular function pointer out of it till the support was added.

Random advice: if you're using VC++2013, don't try this (using int for simplicity):


vector<shared_ptr<int>> = { make_shared<int>(5), make_shared<int>(5) };

Due to some really weird bug with their initializer list, the first element of a container of shared_ptr will have the references reduced to 0 and be deleted.

Almost as funny as seeing their high_resolution_clock:

typedef system_clock high_resolution_clock;

No, their system clock doesn't happen to already be high resolution.

f@dzhttp://festini.device-zero.de


Lamdas are scary sounding then they really are.

--some awesome explanations--

That was the single best explanation I've read about those damn lambda expressions since I've first heard about them and pointlessly tried to grok them. Thank you very much!

-----------------------------Sancte Isidore ora pro nobis !


Lamdas are scary sounding then they really are.

--some awesome explanations--

That was the single best explanation I've read about those damn lambda expressions since I've first heard about them and pointlessly tried to grok them. Thank you very much!

Glad to have helped you. Now just wish I hadn't made a grammar mistake, especially in the body of text you quoted. smile.png

I think part of the problems with lambdas is they come from the math-world, and in math-world, language has a very precise meaning. Therefore reading descriptions of mathematical constructs is often like solving a rubik's cube using your toes. Often translating from technical language to less accurate but easier to grok natural language is all that is in the way of people "getting it".

Side-remark: what the hell is it with the lamda ... erm ... lambda ... i swear, i always automatically write it wrong.

Thankfully, all the browsers i use have spell checkers built in for ages already - and they happily point out that i mistyped lamda ... again.

This topic is closed to new replies.

Advertisement