James
James

Reputation: 9278

Are static lambdas of any use/unwise?

I'm trying to track a very difficult to reproduce bug. I have a pool of items and use the following to automatically check the pool items back in when the client has finished with them:

typedef std::shared_ptr<T> Handle;

Handle MyPool::checkOut()
{
    static const auto CheckInDeleter = [this](T* item)
    {
        this->checkIn(item);
    };

    return Handle(item, CheckInDeleter);
}

My question is is this unsafe? Will this be assigned to the first pool that checks out an item?

Upvotes: 3

Views: 159

Answers (1)

zch
zch

Reputation: 15278

Yes, static function-scope variables are assigned once,so there will be only one lambda object, that will refer to first MyPool that has checkOut member function called. It is probably not what you need, so just remove static.

Upvotes: 5

Related Questions