如何使用lambda和功能作为unique_ptr的自定义删除器

费多罗夫7890

我正在测试的自定义删除器unique_ptr奇怪的是,只有作为函数对象的删除器才能正常工作。如果将它们替换为函数或lambda,程序将无法编译。我究竟做错了什么?

这是我完整的测试程序

#include <iostream>
#include <memory>

using namespace std;


class Vehicle {
public:
    Vehicle(){ cout<<"Vehicle constructor..."<<endl;}
    virtual ~Vehicle(){cout<<"~Vehicle destructor..."<<endl;}
    virtual void go()=0;
};

class Car:public Vehicle {
    public:
    void go() override {
        cout<<"Going by car..."<<endl;
    }
};

class Bicycle:public Vehicle {
    public:
    void go() override {
        cout<<"Going by bicycle..."<<endl;
    }
};

// Custom deleters
auto CustomLambdaDeleter = [](Vehicle* v){
    cout<<"Custom lambda deleter called..."<<endl;
    delete v;
};

void CustomFunctionDeleter(Vehicle* v){
    cout<<"Custom function deleter called..."<<endl;
    delete v;
}

struct CustomFunctorDeleter
{
    void operator()(Vehicle* v ) const {
        cout<<"Custom functor deleter called..."<<endl;
        delete v;
    }
};


// Doesn't compile
//using VehiclePtr = unique_ptr<Vehicle, decltype(CustomLambdaDeleter)>;
// Doesn't compile
//using VehiclePtr = unique_ptr<Vehicle, decltype(&CustomFunctionDeleter)>;
// Works ok
using VehiclePtr = unique_ptr<Vehicle, CustomFunctorDeleter>;


class VehicleFactory {
public:
    static VehiclePtr createVehicle(string type){
        VehiclePtr vptr;
        if("bicycle"==type) {
            vptr.reset(new Bicycle());
            // This also works
            // vptr= (VehiclePtr) new Bicycle();
            return vptr;
        }
        else if("car"==type) {

           vptr.reset( new Car());
           return vptr;
        }

        return nullptr;
    }
};


void vehicleFactoryTest(){
    cout<<"* Starting vehicleFactoryTest()..."<<endl;
    auto firstVehicle = VehicleFactory::createVehicle("bicycle");
    firstVehicle->go();
    auto newCar = VehicleFactory::createVehicle("car");
    newCar->go();
}

int main(int, char **)
{

    vehicleFactoryTest();
    return 0;
}
普雷托里亚人

问题不在于任何一个

using VehiclePtr = unique_ptr<Vehicle, decltype(CustomLambdaDeleter)>;

要么

using VehiclePtr = unique_ptr<Vehicle, CustomFunctorDeleter>;

那两个人自己编译。问题在于下面的行createVehicle

VehiclePtr vptr;

在这里,您将默认构造一个unique_ptr,在使用lambda删除程序时将不会编译,因为lambda无法默认构造。所以你需要

VehiclePtr vptr{nullptr, CustomLambdaDeleter};

在使用函数指针的情况下,您尝试unique_ptr使用nullptr删除器默认构造,这是不允许的。修复方法类似,在这种情况下,您需要将指针传递给函数。

VehiclePtr vptr{nullptr, CustomFunctionDeleter};

您还会在中的最终return语句中犯类似的错误createVehicle将该行更改为

return vptr;

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何制作unique_ptr和自定义删除器的副本

混淆使用unique_ptr和自定义删除器

unique_ptr 的有状态自定义删除器

配备有功能指针作为自定义删除器的unique_ptr是否与shared_ptr大小相同?

unique_ptr,自定义删除器和零规则

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

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

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

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

如何为由 unique_ptr 管理的数组编写自定义删除器?

使用带有unique_ptr的自定义删除器

使用自定义删除器在地图中存储unique_ptr

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

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

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

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

将具有自定义删除器的unique_ptr移到shared_ptr

智能指针(unique_ptr)自定义删除器错误C2027和C2338

具有自定义删除器的unique_ptr构造函数被删除

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

如何为包装需要2个参数的ac函数的unique_ptr类成员创建自定义删除器?

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

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

初级 5 版。来自自定义删除器函数的 unique_ptr 构造函数

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

了解采用自定义删除器的unique_ptr的构造函数

指向重载静态成员的函数指针 - 在 unique_ptr 中用作自定义删除器

初始化传递给unique_ptr自定义删除器的函子

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