Can anybody explain how "self_( this, []( ... ) {} )
" works?
struct Parent {
std::shared_ptr<Parent> self_;
Parent() : self_( this, []( ... ) {} ) {}
operator std::shared_ptr<Parent>() const { return self_; }
}
Can anybody explain how "
self_( this, []( ... ) {} )
" works?
self_
, member of Parent
, is a std::shared_ptr<Parent>
.
With
self_( this, []( ... ) {} )
is initialized using a constructor with two arguments:
(1) the pointer shared, this
, a pointer to the same object containing self_
(so the name)
(2) the function that is called when the self_
object is deleted
Observe the function: []( ... ) {}
It's a lambda function that can accept all (...
, the old-C variadic way) and do nothing (the body is empty).
This (the do-nothing deleter) is done because when the last shared pointer with a specific value is destroyed, nothing has to be done over a this
pointer.