vendredi 12 janvier 2018

Usage of std::unique_ptr in combination with custom free-operations

I have a class with a constructor and a destructor and the following code for both:

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);
        }
}

I would like to replace the pointers with std::unique_ptr-variables, but I do not understand how to implement the free-functions. According to some other questions the following way should work:

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
}

Is that correct, or did I forget something?

Aucun commentaire:

Enregistrer un commentaire