Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于对象池模式SimpleObjectPool.hpp中的问题 #31

Open
danniel2016 opened this issue Jun 11, 2019 · 1 comment
Open

关于对象池模式SimpleObjectPool.hpp中的问题 #31

danniel2016 opened this issue Jun 11, 2019 · 1 comment

Comments

@danniel2016
Copy link

代码如下
std::unique_ptr<T, DeleterType> get()
{
if (pool_.empty())
{
throw std::logic_error("no more object");
}

    //every time add custom deleter for default unique_ptr
    std::unique_ptr<T, DeleterType> ptr(pool_.back().release(), [this](T* t)
    {
        pool_.push_back(std::unique_ptr<T>(t));
    });

    pool_.pop_back();
    return std::move(ptr);
}

请教下,代码中使用的lambda表达式中这一句pool_.push_back这句的作用是什么呢?一直没看懂

@guyueshui
Copy link

就是将用完的对象再放回池子里啊。你看申请的时候是从池子里面取出来,用完之后,由智能指针控制其析构,但它的析构就是将该对象重新放到池子里!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants