为什么 auto_ptr不定义了operator!()吗?

发布于 2024-09-07 20:52:18 字数 271 浏览 6 评论 0原文

标题几乎总结了我的问题。为什么不能执行以下操作来检查空指针?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红焚 2024-09-14 20:52:18

看来它的设计有错误。这将在 C++0x 中修复。 unique_ptr(替换auto_ptr)包含显式运算符 bool() const;

引用新 C++ 标准:

类模板 auto_ptr 已弃用。 [注意:类模板 unique_ptr (20.9.10) 提供了
更好的解决方案。 ——尾注]


一些说明:
问:a.get() == 0有什么问题?
A:a.get()==0没有任何问题,但是智能指针可以让您像真正的指针一样使用它们。额外的operator bool()为您提供了这样的选择。我认为,不推荐使用 auto_ptr 的真正原因是它没有直观的设计。但新标准中 unique_ptroperator bool 意味着没有理由不使用它。

Seems to be there was an error in its design. This will be fixed in C++0x. unique_ptr (replacement for auto_ptr) contains explicit operator bool() const;

Quote from new C++ Standard:

The class template auto_ptr is deprecated. [Note: The class template unique_ptr (20.9.10) provides a
better solution. —end note ]


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. Additional operator bool() gives you such a choice. I think, that the real reason for making auto_ptr deprecated is that is has has not intuitive design. But operator bool for unique_ptr in the new Standard means that there are no reasons not to have it.

洛阳烟雨空心柳 2024-09-14 20:52:18

简而言之,它应该定义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 the operator bool() conversion operator defined which can be negated with operator !(). That will let your if(!p) compile and work as expected.

是你 2024-09-14 20:52:18

布尔转换存在问题。它允许几乎总是令人痛苦的语法。

幸运的是,有一个解决方案:Safe Bool 习惯用法。

转换为 bool 的问题是隐式转换很危险。

std::auto_ptr<T> p = ..., q = ....;

if (p < q) // uh ?

因此,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.

std::auto_ptr<T> p = ..., q = ....;

if (p < q) // uh ?

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) and if (!p) then make sense, but if (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.

被你宠の有点坏 2024-09-14 20:52:18

我怀疑是因为预计将 auto_ptr 传递给 null 的情况很少见,以避免添加额外的接口,并在实际检查 null 时使其明确。

I suspect because it was expected that passing around auto_ptrs to null would be a rare case, to avoid adding extra interface, and to make it explicit when actually checking for null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文