为什么 auto_ptr不定义了operator!()吗?
标题几乎总结了我的问题。为什么不能执行以下操作来检查空指针?
auto_ptr<char> p( some_expression );
// ...
if ( !p ) // error
必须这样做:
if ( !p.get() ) // OK
为什么 auto_ptr
不简单地定义 operator!()
?
The Title pretty much sums up my question. Why can't the following be done to check for a null pointer?
auto_ptr<char> p( some_expression );
// ...
if ( !p ) // error
This must be done instead:
if ( !p.get() ) // OK
Why doesn't auto_ptr<T>
simply have operator!()
defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来它的设计有错误。这将在 C++0x 中修复。
unique_ptr
(替换auto_ptr
)包含显式运算符 bool() const;
引用新 C++ 标准:
一些说明:
问:
a.get() == 0
有什么问题?A:
a.get()==0
没有任何问题,但是智能指针可以让您像真正的指针一样使用它们。额外的operator bool()
为您提供了这样的选择。我认为,不推荐使用auto_ptr
的真正原因是它没有直观的设计。但新标准中unique_ptr
的operator bool
意味着没有理由不使用它。Seems to be there was an error in its design. This will be fixed in C++0x.
unique_ptr
(replacement forauto_ptr
) containsexplicit operator bool() const;
Quote from new C++ Standard:
Some clarification:
Q: What's wrong with
a.get() == 0
?A: Nothing is wrong with
a.get()==0
, but smart pointers lets you work with them as they were real pointers. Additionaloperator bool()
gives you such a choice. I think, that the real reason for makingauto_ptr
deprecated is that is has has not intuitive design. Butoperator bool
forunique_ptr
in the new Standard means that there are no reasons not to have it.简而言之,它应该定义
operator !()
。auto_ptr
不是一个设计非常好的容器。 boost 中的智能指针定义了operator bool()
转换运算符,可以使用operator !()
对其取反。这将使您的if(!p)
编译并按预期工作。Simply put, it should have
operator !()
defined.auto_ptr
is not a very well designed container. The smart pointers in boost have theoperator bool()
conversion operator defined which can be negated withoperator !()
. That will let yourif(!p)
compile and work as expected.布尔转换存在问题。它允许几乎总是令人痛苦的语法。
幸运的是,有一个解决方案:Safe Bool 习惯用法。
转换为 bool 的问题是隐式转换很危险。
因此,operator bool() const 是令人厌恶的。要么提供显式方法...要么使用安全 bool 习惯用法。
该习惯用法的想法是为您提供一个具有极少操作子集的类型实例,并且几乎不会出现隐式转换会给您带来麻烦的情况。这是通过使用指向成员函数的指针来完成的。
像
if (p)
和if (!p)
这样的操作是有意义的,但是if (p < q)
将无法编译。仔细阅读完整解决方案的链接,您将意识到为什么不使用
operator bool() const
是个好主意。There is an issue with boolean conversion. It allows syntaxes that are nearly always a pain.
There is, luckily, a solution: the Safe Bool idiom.
The problem with a conversion to
bool
is that implicit conversion is dangerous.Therefore,
operator bool() const
is an abomination. Either you provide an explicit method... or you use the safe bool idiom.The idea of the idiom is to give you an instance of a type with a pretty minimal subset of operations and almost no case where the implicit conversion will get you into trouble. This is done by using a pointer to member function.
Operations like
if (p)
andif (!p)
then make sense, butif (p < q)
will fail to compile.Read the link thoroughly for the complete solution, and you'll realize why it was a good idea not to have
operator bool() const
.我怀疑是因为预计将 auto_ptr 传递给 null 的情况很少见,以避免添加额外的接口,并在实际检查 null 时使其明确。
I suspect because it was expected that passing around
auto_ptr
s to null would be a rare case, to avoid adding extra interface, and to make it explicit when actually checking for null.