我正在测试的自定义删除器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] 删除。
我来说两句