Pretty much just the topic title.
Wow, talk about confusing. It's like the creators of c++11 standard wanted to make it seem convoluted.
Basically, I think I just got taken back to school.
Pretty much just the topic title.
Wow, talk about confusing. It's like the creators of c++11 standard wanted to make it seem convoluted.
Basically, I think I just got taken back to school.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
The hardest thing about lambdas is how they capture the variables used in the function, which is honestly just something that I look up. I imagine that they were designed to be that complicated because parameters are complicated in C++: you have a number of semantics to choose from, and in other languages, the problem is eliminated by the environment. Other than that, it's like writing a function.
I don't find lambdas difficult at all. apart from the capture clause, their syntax is quite like the rest of C++. it's a new major feature of the language. you can't expect to be fully comfortable with it overnight. just play with them and learn as much as you can. you'll eventually get to the point where it's just as natural to write them as any other part of the language.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
The lambda allows you to write a functor inline, so that the code of the functor is found at the same spot where the functor is used. Before lambdas, functors had to be implemented as classes elsewhere in the source code, making it hard to know what the functor does without scrolling away to another section of code.
That's really all they're for. You can achieve all the same things a lambda does without using lambda, it's just not as nice.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Yep, they're especially useful for situations where you need to do some repetitive task within a function.
Achieving something like that was always such a pain in the arse with C++03...Code:#include <iostream> void wtf( int arg ) { std::cout << arg << " - WTF?!!!" << std::endl; } int fun( int value ) { int result = 390937; auto cleanup = [&result, value]( ) { result = 0; wtf( value ); }; if( value < 314821 ) cleanup( ); else if( value > 357761 ) cleanup( ); else result *= value; return result; } int main( ) { fun( 280601 ); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Wait Vart, are you serious about that statement? I mean, I laughed but I could almost see it being funny because it's true.
Your example is not particularly illustrative of anything except poor coding style. This version (without lambda) is much cleaner than your version
Of course, the usage of magic values is poor style, but that's unrelated to your point or mine.Code:int fun( int value ) { int result = 0; if ( value < 314821 || value > 357761 ) wtf(value); else result = 390937 * value; return result; }
Please try to understand that it was just an abstract example, grumpy. I imagine that must be very hard for your pedantic mind to grasp, but I'm sure that with a little effort even you can appreciate the point of the demonstration.Originally Posted by grumpy
Rather, since the numbers are meaningless in the context of the example given, assigning labels to them would only serve to confuse the reader into thinking that the actual values were somehow tied to the point being made. Otherwise I most certainly would have given them concrete names, such as "A001262_26" and so forth...Originally Posted by grumpy
Last edited by Sebastiani; 09-28-2013 at 12:01 AM.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
I'd say that a fairly common use case would be specifying a predicate when using various standard algorithms.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I thought predicate technically meant a boolean function.
(Or is that just lisp terminology? ..where appending _p after a function signifies that it is a yes/no question.)
Last edited by manasij7479; 09-28-2013 at 03:44 AM.
A predicate does return bool, or a value convertible to bool.