空别名shared_ptr是否可以代替无操作删除shared_ptr? [英] Is an empty aliasing shared_ptr a good alternative to a no-op deleting shared_ptr?

查看:62
本文介绍了空别名shared_ptr是否可以代替无操作删除shared_ptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我需要具有无操作删除程序的 shared_ptr 实例,因为API希望使用 shared_ptr 实例,它想存储一段有限的时间,但是我得到了一个原始指针,不允许我拥有比运行时间更长的时间。

Sometimes I need shared_ptr instances that have a no-op deleter, because an API expects a shared_ptr instance that it wants to store for a limited time but I am given a raw pointer that I am not allowed to own for a time larger than what I am running for.

在这种情况下,我一直在使用无操作删除器,例如 [](const void *){} ,但是今天我发现,还有另一种选择,使用(或滥用?)别名为 shared_ptr 的构造函数

For this case, I have been using a no-op deleter, such as [](const void *){}, but today I found that there's another alternative to that, using (or abusing?) the aliasing constructor of shared_ptr:

void f(ExpectedClass *ec) {
   std::shared_ptr<ExpectedClass> p(std::shared_ptr<void>(), ec);
   assert(p.use_count() == 0 && p.get() != nullptr);
   apiCall(p);
}

我的问题是,这样做的更好方法是什么?为什么?性能期望是否相同?使用无操作删除器时,我希望为删除器的存储和引用计数支付一些费用,当将别名构造函数与空的 shared_ptr

My question is, what is the better way to do this and why? Are the performance expectations the same? With a no-op deleter I expect to pay some cost for the storage of the deleter and reference count, which doesn't appear to be the case when using the aliasing constructor with the empty shared_ptr.

推荐答案

关于性能,以下基准显示了不稳定的数字:

Concerning performance, the following benchmark shows erratic figures:

#include <chrono>
#include <iostream>
#include <limits>
#include <memory>

template <typename... Args>
auto test(Args&&... args) {
    using clock = std::chrono::high_resolution_clock;
    auto best = clock::duration::max();

    for (int outer = 1; outer < 10000; ++outer) {
        auto now = clock::now();

        for (int inner = 1; inner < 20000; ++inner)
            std::shared_ptr<int> sh(std::forward<Args>(args)...);

        auto time = clock::now()-now;
        if (time < best) {
            best = time;
            outer = 1;
        }
    }

    return best.count();
}

int main()
{
    int j;

    std::cout << "With aliasing ctor: " << test(std::shared_ptr<void>(), &j) << '\n'
              << "With empty deleter: " << test(&j, [] (auto) {});
}

在我的计算机上使用 clang ++ -march = native输出-O2

With aliasing ctor: 11812
With empty deleter: 651502

具有相同选项的海湾合作委员会给出了更大的比率5921:465794。

和Clang与 -stdlib = libc ++ 一起产生12:613175。

GCC with identical options gives an even larger ratio, 5921:465794.
And Clang with -stdlib=libc++ yields a whopping 12:613175.

这篇关于空别名shared_ptr是否可以代替无操作删除shared_ptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆