4 ms·
Lambdas are compiler friendly. They reliably inline unlike standalone functions. It's not laziness, it's an optimization.
by zelly 6y ago
Lambdas are compiler friendly. They reliably inline unlike standalone functions. It's not laziness, it's an optimization.
- moomin 6y agoC# has inner (local) functions which are close in semantics to lambdas but have names. I didn’t think I’d use them at first, but actually I use them all the time and often prefer them to raw lambdas.
- zelly 6y ago#include <cstdio> int main() { auto square = [](double x) -> double { return x*x; }; printf("%f\n", square(10)); return 0; } since 2011
- notacoward 6y ago> They reliably inline unlike standalone functions. No. They don't. Non-constexpr ones can't. And lambdas with captures that outlive their creators are notoriously painful for compilers and libraries to support.
- ncmncm 6y agoYet, they do. Lambdas with captures are explicitly modeled as a struct holding the captured values, and a member function that takes its implicit "this". That function, with its implementation fully exposed, is as easily inlined as any other. The struct is moved or copied as needed to keep it live after the originating context evaporates.
- notacoward 6y agoI stand corrected. OTOH, "functionally equivalent but makes debugging or verification harder" isn't actually a strong argument in favor of lambdas.
- ncmncm 6y agoFortunately they don't need a strong argument. Convenience and minimized boilerplate suffices.