[英]std::unique_ptr custom deleter
Reference Well, how does the custom deleter of std::unique_ptr work? 参考那么,std :: unique_ptr的自定义删除器如何工作?
Constructor 建设者
std::unique_ptr<ErrorHandling> error_;
RecursiveDescentParser::RecursiveDescentParser(std::string inputStream, bool fileService,
boost::optional<std::string> filePathName, std::ofstream &writer){
if (fileService == true){
error_(new ErrorHandling(fileService, writer)); <---- compiler error
}
else{
error_(new ErrorHandling(fileService, std::ofstream())); <---- compiler error
}
}
Compiler error 编译器错误
Error 1 error C2247: 'std::default_delete<_Ty>::operator ()' not accessible because 'std::unique_ptr<_Ty>' uses 'private' to inherit from 'std::_Unique_ptr_base<_Ty,_Dx,_Empty_deleter>'
Cause of error described here . 错误原因在这里描述。
I decided since 'std::default_delete<_Ty>::operator ()
is private
because child class ( std::unique_ptr
in this case) has specified private inheritance
that I would write my own custom deleter. 我决定因为
'std::default_delete<_Ty>::operator ()
是private
因为子类(在这种情况下为std::unique_ptr
)指定了private inheritance
,因此我将编写自己的自定义删除器。 Problem is I am too uncomfortable with the syntax and notation to succeed. 问题是我对语法和符号太不满意而无法成功。
This line 这条线
error_(new ErrorHandling(fileService, writer));
is an error because unique_ptr
doesn't have an operator()
. 错误是因为
unique_ptr
没有operator()
。 The error message is a bit misleading, because its base class seems to have one (but luckily private). 错误消息有点误导,因为它的基类似乎有一个(但幸运的是私有的)。
You probably intended 你可能打算
error_.reset(new ErrorHandling(fileService, writer));
which makes the unique_ptr
own a new object. 这使得
unique_ptr
拥有一个新对象。
The problem is that you're trying to use the function-call operator on a unique_ptr
, which is not a valid thing to do. 问题是您试图在
unique_ptr
上使用函数调用运算符,但这不是正确的做法。 Then some confusion is caused because your particular implementation happens to have an inaccessible operator, rather than no operator at all, giving a weird error message. 然后会引起一些混乱,因为您的特定实现恰好有一个无法访问的运算符,而不是根本没有运算符,从而给出了奇怪的错误消息。
I assume you're actually trying to reset error_
to own a new pointer: 我假设您实际上是在尝试将
error_
重置为拥有新的指针:
error_.reset(new ErrorHandling(fileService, writer));
// ^^^^^^
UPDATE: if you do want a custom deleter (which, to reiterate, you don't in this situation), it might look like: 更新:如果您确实想要一个自定义删除器(重申一下,您不在这种情况下),它可能看起来像:
struct ErrorHandlingDeleter
{
void operator()(ErrorHandling * p) {/* do something to release the pointer */}
};
std::unique_ptr<ErrorHandling, ErrorHandlingDeleter> error;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.