我正在研究 std::tr1::shared_ptr<> 如何提供转换为 bool 的能力。过去,当我尝试创建一个可以转换为 bool 的智能指针作为简单的解决方案时,我就陷入了困境,即
operator bool() {
return m_Ptr!=0;
}
通常最终会隐式转换为指针类型(大概是通过类型提升),这通常是不可取的。 boost 和 Microsoft 实现似乎都使用了涉及转换为 unspecified_bool_type()
的技巧。谁能解释一下这种机制是如何工作的以及它如何防止隐式转换为底层指针类型?
I was looking into how std::tr1::shared_ptr<>
provides the ability to cast to bool. I've got caught out in the past when trying to create a smart pointer that can be casted to bool as the trivial solution, ie
operator bool() {
return m_Ptr!=0;
}
usually ends up being implicitly castable to the pointer type (presumably by type promotion), which is generally undesirable. Both the boost and Microsoft implementations appear to use a trick involving casting to an unspecified_bool_type()
. Can anyone explain how this mechanism works and how it prevents implicit casting to the underlying pointer type?
发布评论
评论(3)
问题中描述的技术是safe bool idiom。
从 C++11 开始,不再需要该习惯用法。该问题的现代解决方案是在运算符上使用
explicit
关键字:The technique described in the question is the safe bool idiom.
As of C++11, that idiom is no longer necessary. The modern solution to the problem is to use the
explicit
keyword on the operator:这个技巧是这样运作的。您可以在智能指针类型中定义所有这些内容(在本例中为
shared_ptr
):ptr_
是智能指针类中的本机指针。正如您所看到的,
unspecified_bool_type
是一个无法被任何外部代码访问的类型的typedef
,因为Tester
是一个私有结构。但是调用代码可以使用这种(隐式)转换为指针类型并检查它是否为空。在 C++ 中,可以用作bool
表达式。The trick works like this. You define all this inside your smart pointer type (in this case,
shared_ptr
):ptr_
is the native pointer inside the smart pointer class.As you can see,
unspecified_bool_type
is atypedef
to a type that cannot be accessed by any external code, sinceTester
is a private struct. But calling code can use this (implicit) conversion to a pointer type and check whether it is null or not. Which, in C++, can be used as abool
expression.通常它返回的是一个成员指针。成员指针可以像
bool
一样对待,但不支持bool
所支持的许多隐式转换。Usually what it returns is a member pointer. Member pointers can be treated like a
bool
but don't support many of the implicit conversions whichbool
does.