我可以使用 lambda 作为自定义删除器吗?事实上,我使用了一个 C++ 库,其中很多类使用创建/发布 API 进行实例生命管理(参见下面的示例)
class FileInterface
{
public:
virtual ~FileInterface() {}
virtual bool isValid() = 0 ;
virtual void release() = 0;
};
class RealFile : public FileInterface
{
public:
static int createFileInterface(const std::string& filename, FileInterface*& pFileInst)
{
try {
pFileInst = new RealFile(filename);
} catch (...){
return -1;
}
return 0;
}
virtual bool isValid() { return (m_pFile != NULL);}
virtual void release() { delete this;}
protected:
RealFile(const std::string& filename) : m_pFile(NULL) { m_pFile = fopen(filename.c_str(), "wb"); if(m_pFile == NULL) {throw std::runtime_error("error while opening file.");} }
~RealFile() {
std::cout << "DTOR" << std::endl;
fclose(m_pFile);
}
private:
FILE* m_pFile;
};
所以要使用我需要自己处理的那种类release
(在每次返回,抛出等......)。
FileInterface* pFile = nullptr;
int ret = RealFile::createFileInterface("test.bin", pFile);
std::cout << "isValid = " << pFile->isValid() << std::endl;
pFile->release();
所以我会使用智能指针来处理创建/发布逻辑。我的第一步是处理删除器,它工作正常
auto smartDeleter = [](FileInterface* ptr){ptr->release();};
FileInterface* pFile = nullptr;
int ret = RealFile::createFileInterface("test.bin", pFile);
std::unique_ptr<FileInterface, decltype(smartDeleter)> smartFile(pFile);
std::cout << "isValid = " << smartFile->isValid() << std::endl;
但在第二步中,我为创建逻辑编写了一个 lambda:
auto smartAllocator = [](const std::string& filename){
FileInterface* pFile = nullptr;
int ret = RealFile::createFileInterface(filename, pFile);
if (ret != 0) return nullptr;
else return pFile;
};
编译器报告错误:
CreateReleasePattern.cpp(65): error C3487: 'FileInterface *': all return expressions in a lambda must have the same type: previously it was 'nullptr'
1>CreateReleasePattern.cpp(65): error C2440: 'return' : cannot convert from 'FileInterface *' to 'nullptr'
1> nullptr can only be converted to pointer or handle typesCreateReleasePattern.cpp(65): error C3487: 'FileInterface *': all return expressions in a lambda must have the same type: previously it was 'nullptr'
1>CreateReleasePattern.cpp(65): error C2440: 'return' : cannot convert from 'FileInterface *' to 'nullptr'
1> nullptr can only be converted to pointer or handle types
我该如何解决?我可以在 FileInterface 上写一个可转换的运算符吗?