std :: unique_ptr与自定义自由操作结合使用

arc_lupus

我有一个带有构造函数和析构函数的类,以及下面两个代码:

class propagation_module{
    private:
                gsl_interp_accel *xa;
                interp2d_spline *interp_s;
    public:
        propagation_module()
        {
            std::vector<int> p_N = {1, 2, 3}, z_vec = {1, 2, 3};
            xa = gsl_interp_accel_alloc();
            interp_s = interp2d_spline_alloc(interp2d_bicubic, p_N.size(), z_vec.size());
        }
        ~propagation_module(){
            gsl_interp_accel_free(xa);
            interp2d_spline_free(interp_s);
        }
}

我想用std :: unique_ptr-variables替换指针,但是我不明白如何实现自由功能。根据其他一些问题,以下方法应该起作用:

class propagation_module
{
    private: 
        std::unique_ptr<gls_interp_accel, decltype(&gsl_interp_accel_free)> xa;
        std::unique_ptr<interp2d_spline, decltype(&interp2d_spline_free)> interp_s;
    public:
        propagation_module()
        {
            //Same as above
        }
        //No destructor necessary
}

是正确的,还是我忘记了什么?

Angew不再为SO感到骄傲

你在那儿 默认情况下,std::unique_ptrunique_ptr创建时未传递任何删除器时它将默认构造其删除器您为删除器提供了正确的类型,但也需要提供它们的

public:
    propagation_module()
    {
        std::vector<int> p_N = {1, 2, 3}, z_vec = {1, 2, 3};
        xa = {gsl_interp_accel_alloc(), &gsl_interp_accel_free};
        interp_s = {interp2d_spline_alloc(interp2d_bicubic, p_N.size(), z_vec.size()), &interp2d_spline_free};
    }

另外,如果您不想每次创建唯一指针时都必须提供正确的删除器,则可以创建默认可构造的包装器来调用正确的函数:

struct GslInterpAccelFree
{
  void operator() (gsl_interp_accel *p) const { gsl_interp_accel_free(p); }
};

struct Interp2dSplineFree
{
  void operator() (interp2d_spline *p) const { interp2d_spline_free(p); }
};

class propagation_module
{
    private: 
        std::unique_ptr<gls_interp_accel, GslInterpAccelFree> xa;
        std::unique_ptr<interp2d_spline, Interp2dSplineFree> interp_s;
    public:
        propagation_module()
        {
            std::vector<int> p_N = {1, 2, 3}, z_vec = {1, 2, 3};
            xa.reset(gsl_interp_accel_alloc());
            interp_s.reset(interp2d_spline_alloc(interp2d_bicubic, p_N.size(), z_vec.size()));
        }
        //No destructor necessary
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用std :: function对象将自定义删除器传递给std :: unique_ptr

无法使用带有std :: move的自定义删除器插入std :: unique_ptr

使用typedef为std :: unique_ptr指定自定义默认删除器

C ++ 11:使用自定义Lambda Deleter返回std :: unique_ptr

std :: unique_ptr使用带有很少参数的自定义删除器

我可以使用自定义删除器简洁地声明std :: unique_ptr吗?

如何将自定义删除器与std :: unique_ptr成员一起使用?

使用内存池中的自定义删除器将 std::unique_ptr 返回到抽象类型

如何安全地重载std :: unique_ptr的自定义删除程序?

std :: unique_ptr <T []>和自定义分配器删除器

未调用std :: unique_ptr中的自定义删除器

带有lambda自定义删除程序的std :: unique_ptr无法编译

std :: unique_ptr,自定义删除器和类型更改

如何在C ++ 11中返回包含自定义删除器的std :: unique_ptr?

可以将std :: unique_ptr子类化以透明地应用自定义删除程序吗?

在 Qt 上使用 std::unique_ptr

结合 std::unique_ptr 和命名构造函数

使用什么std :: optional或std :: unique_ptr

在 QVector<std::unique_ptr<Type>> 上使用 std::find

std :: unique_ptr :: release()与std :: move()

std :: unique_ptr与std :: map

使用std :: unique_ptr <T>&代替std :: unique_ptr <T>有什么优势吗?

std :: unique_ptr的自定义删除程序是否是手动调用析构函数的有效位置?

如何制作将函数包装在noexcept可检测的可调用对象中的类模板,以用作std :: unique_ptr自定义删除器?

使用自定义比较器的std :: set操作

参数std :: unique_ptr <T> &&的std :: move或std :: forward

使用 std::unique_ptr 使用 CRTP 进行转换

std :: auto_ptr到std :: unique_ptr

如何在std :: copy中使用unique_ptr?