C++ Weekly - Ep 126 - Lambdas With Destructors

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

Upcoming Workshops:

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

C++ Weekly, 2029: "Concrete types deprecated, everything is a lambda now"

superscatboy
Автор

You are one of the few people I will watch, going back slowly into all of your content! (I’ve been doing C/C++ for 20+ years)

ChnSailor
Автор

One can never have too many videos about lambdas. I'm really enjoying every one of them.

vertigo
Автор

Might come in handy if we ever have a "most ugly C++" competition

devluz
Автор

Lambda, which capture another lambda inline, which have structure declaration and usage. I was struck to the core of my soul by the fact that it is possible at all !

Is it possible to unsee this?! :D Why not add few levels of nesting templates, make them mutable, and mix with SFINAE, type-traits and move-semantics? :D

applicative_functor
Автор

I could see it used for timing, something like function profiling. It's not that necessary anymore, but you have the lambda grab a mutex on creation and then release it on destruction, that way you don't have to worry about things like mutexes not being release in the even exceptions and things like that.

goshisanniichi
Автор

I had to find this video again just to convince myself it wasn't a dream

NonTwinBrothers
Автор

if i were to use something like that id most likely just put together a struct with operator() or use a finally object from some utility library

rabbitdrink
Автор

This looks like it could have a use in a SCOPE_EXIT{ } implementation.

ukaszdrozdz
Автор

Or you can just write a regular, old school functor struct directly and bypass both the unnecessary extra wrapping layers AND the write only lambda code.

You can still do things with those that no lambda can, like a recursive calls. And in this case it’d be faster too.

zackyezek
Автор

What's the difference between defining i in the capture clause and defining it as a local variable within the lambda function body?

YTonYahoo
Автор

I find the name misleading, because the destructor does not destroy the lambda.

shushens
Автор

That lambda has no destructor. That struct S has a destructor.

DusanJovanovicDBJ
Автор

If I ever happen to have a container of mutexes, I'll make sure to use a "destructor lambda" with std::for_each to lock them all :D

AxelStrem
Автор

Wouldn't a simpler lambda with a destruction be [s=std::string("Hello")](){}?

danielrhouck
Автор

I'm puzzled by that struct S being in scope after the outer lambda.

X_Baron
Автор

Not sure this is really a “destructor” for the lambda … it gets called twice . Also not clear why you’d need it

foomoo
Автор

Why do these kind of programming technics remind me Javascript?

alexeiz
Автор

I think it can be useful for debugging and optimizing. When I write functions taking a lambda function as a parameter, I always pass them by value, because in most cases the lambdas just capture a reference to all variables, this should be as fast as copying a pointer, but when other people use the functions, they might capture variables by value, this could help me to improve the code, so that lambdas are not copied all over the place.

But do you think it is better to pass lambdas by value, by l-value reference or universal reference when writing functions like in the <algorithm>-header?

cmdlp
Автор

I usually use a class that is construct able with a std::function only to make a scope guard for when using a c api that requires cleanup. Or if possible I use boost scope exit

markusbock
welcome to shbcf.ru