C++11:使用自定义 Lambda 删除器返回 std::unique_ptr

C++11: Returning std::unique_ptr with Custom Lambda Deleter

问题:

有谁知道 return a std::unique_ptr 的方法,它使用定义为 lambda 的自定义删除器?如果做不到这一点,有人知道解决以下问题的更好的整体方法吗?

背景:

我正在研究 C++11 数据库连接池。这个想法很简单,但是我似乎停留在实际的连接指针上 returning.

我的连接池的总体设计是预先创建一些连接,这些连接存储在 std::vectorbool 中以指示给定连接是否可用:

static std::vector<std::pair<DataStore*, bool>> connList;

计划应要求 return std::unique_ptr 带有自定义删除器的对象。这个删除器实际上不会释放连接,而是会重置 bool 以允许将连接分发给后续请求者。这样,class的消费者就不用记得释放连接对象了。

我遇到的问题是我不确定如何实际 return 这个 std::unique_ptr 与关联的删除器,这是在模板参数中指定的。 AFAIK,lambda 是捕获重置 bool 所需的向量索引的唯一方法。我目前对这个 lambda 的实现很简单:

auto connDeleter = [index](DataStore* p) { connList[index].second = true; };

我尝试的解决方案:

return std::unique_ptr<DataStore, decltype(connDeleter)>
    (connList[index].first, connDeleter);

不幸的是,这需要 class 的所有外部消费者都知道 connDeleter,这没有任何意义,因为 indexconnList 没有任何意义在相关功能之外。

AFAIK,任何其他解决方案(仿函数或自由函数)都不允许我接收任何类型的索引来重置连接。我可以将指针和索引包装在一个中间对象中,并将解除引用运算符覆盖为 "pass through" 到底层指针。这将允许我从中间对象更新/提取任何内容,但这对我来说真的很老套。也不确定额外对象对性能的影响。

更多信息:

您可以使用 std::function 作为 std::unique_ptr 中的删除器类型。

std::unique_ptr<DataStore, std::function<void(DataStore*)>> get_ptr()
{
    auto deleter = [index](DataStore*) {
        connList[index].second = true;
    });
    return std::unique_ptr<DataStore, std::function<void(DataStore*)>>(new DataStore(), deleter)
}

并告诉您的用户使用

using managed_ptr = std::unique_ptr<DataStore, std::function<void(DataStore*)>>;

我建议使用手动编写删除器的方法。与利用 ::std::function 的方法不同,与 lambda 相比,这种方法不应受到开销的影响:

class
t_MyDeleter
{
    private: ::std::size_t m_index;

    private: t_MyDeleter(void) = delete;

    public: t_MyDeleter(t_MyDeleter const & that) noexcept: m_index{that.m_index} {}

    public: explicit t_MyDeleter(::std::size_t const index) noexcept: m_index{index} {}

    public: void operator ()(DataStore * const p_store) const
    {
        static_cast<void>(p_store); // not used?
        // don't we also need to keep a reference to connList?
        connList[m_index].second = true;
    }
};

using
t_UniquePointerToDataStore = ::std::unique_ptr<DataStore, t_MyDeleter>;

t_UniquePointerToDataStore
Make_DataStore(void)
{
    ::std::size_t index{};
    return(t_UniquePointerToDataStore{connList[index].first, t_MyDeleter{index}});
}

还有 C++14 自动 return 函数的类型推导,可以在不做任何更改的情况下使用 lamba:

auto
Make_DataStore(void)
{
    auto connDeleter = [index](DataStore* p) { connList[index].second = true; };
    return(::std::unique_ptr<DataStore, decltype(connDeleter)>{connList[index].first, connDeleter});
}

即使我更喜欢自定义删除器 class,隐藏删除器的另一种可能性是使用 std::shared_ptr(因此比 std::unique_ptr 有更多的开销):

std::shared_ptr<DataStore> MakeDataStore(int index)
{
    auto connDeleter = [index](DataStore* p) { connList[index].second = true; };

    return std::shared_ptr<DataStore>(connList[index].first, connDeleter);
}