作为条件的智能指针:if (p) 和 if (p.get()) 是否等价?
Smart pointer as condition: are if (p) and if (p.get()) equilavent?
让p
成为一个shared/unique指针。 if (p)
和 if (p.get())
等价吗?
如果不是,在什么情况下这些条件语句或条件语句中的代码会有不同的行为?
从 cppreference 我读到 std::shared_ptr::operator bool
检查是否 get() != nullptr
。这是 operator bool
的确切实现吗?
"exact implementation" 不应该是您关心的问题(它会因编译器而异,因版本而异,并且可能取决于您提供给编译器的选项)
您应该关注 "How will a standards-compliant compiler behave",答案是“是的,if(ptr)
应该始终产生与 if(ptr.get())
相同的结果
来自标准:
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true . A prvalue of type std::nullptr_t can be converted to a prvalue of type bool ; the resulting value is false .
而智能指针算作nullable pointers,有如下要求:
An object p of type P can be contextually converted to bool (Clause [conv]). The effect shall be as if p != nullptr had been evaluated in place of p.
(感谢 T.C。引用)
智能指针 operator bool()
背后的目的是使智能指针在执行有效性检查的常见情况下表现得像常规指针,即在您编写
的情况下
if (myPointer) {
... // Do something
}
确切的实现可能不同。例如,不是调用 get()
并将结果与 nullptr
进行比较,符合标准的实现可以检查存储由 get()
编辑的值 return 的成员变量,或者检查指示指针未指向任何有效内容的私有标志。
但是,对于程序员而言,只要知道当 get()
return 是非空值时,实现将 return true
就足够了,并且当 get()
returns nullptr
.
时它也会 return false
Are if (p)
and if (p.get())
equivalent?
来自this reference(同样适用于unique_ptr
):
Checks if *this
stores a non-null pointer, i.e. whether get() != nullptr
.
是的,if (p)
和 if (p.get())
具有相同的行为。不,迂腐地说它们并不完全等同:前者有一个函数调用,后者有一个不同的函数调用和一个指针比较。实际上,两者都可能生成相同的代码。
If not, in what cases can these conditionals, or the code within the conditionals, behave differently?
它们的行为完全相同。
Is this the exact implementation of operator bool?
这正是实现必须表现的方式。
确切的实现是...定义的实现。您可以阅读标准库实现的源代码来找出答案。
它们的行为完全相同。
为了找出我们可以使用指针空值检查的各种方式,您可以查看以下内容
http://www.artima.com/cppsource/safebool.html
这就是所谓的安全布尔习语。尽管它在 c++11 中似乎已过时,但它很好地揭示了 sharedPtrs 上 bool 操作的使用。
让p
成为一个shared/unique指针。 if (p)
和 if (p.get())
等价吗?
如果不是,在什么情况下这些条件语句或条件语句中的代码会有不同的行为?
从 cppreference 我读到 std::shared_ptr::operator bool
检查是否 get() != nullptr
。这是 operator bool
的确切实现吗?
"exact implementation" 不应该是您关心的问题(它会因编译器而异,因版本而异,并且可能取决于您提供给编译器的选项)
您应该关注 "How will a standards-compliant compiler behave",答案是“是的,if(ptr)
应该始终产生与 if(ptr.get())
来自标准:
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true . A prvalue of type std::nullptr_t can be converted to a prvalue of type bool ; the resulting value is false .
而智能指针算作nullable pointers,有如下要求:
An object p of type P can be contextually converted to bool (Clause [conv]). The effect shall be as if p != nullptr had been evaluated in place of p.
(感谢 T.C。引用)
智能指针 operator bool()
背后的目的是使智能指针在执行有效性检查的常见情况下表现得像常规指针,即在您编写
if (myPointer) {
... // Do something
}
确切的实现可能不同。例如,不是调用 get()
并将结果与 nullptr
进行比较,符合标准的实现可以检查存储由 get()
编辑的值 return 的成员变量,或者检查指示指针未指向任何有效内容的私有标志。
但是,对于程序员而言,只要知道当 get()
return 是非空值时,实现将 return true
就足够了,并且当 get()
returns nullptr
.
false
Are
if (p)
and if(p.get())
equivalent?
来自this reference(同样适用于unique_ptr
):
Checks if
*this
stores a non-null pointer, i.e. whetherget() != nullptr
.
是的,if (p)
和 if (p.get())
具有相同的行为。不,迂腐地说它们并不完全等同:前者有一个函数调用,后者有一个不同的函数调用和一个指针比较。实际上,两者都可能生成相同的代码。
If not, in what cases can these conditionals, or the code within the conditionals, behave differently?
它们的行为完全相同。
Is this the exact implementation of operator bool?
这正是实现必须表现的方式。
确切的实现是...定义的实现。您可以阅读标准库实现的源代码来找出答案。
它们的行为完全相同。
为了找出我们可以使用指针空值检查的各种方式,您可以查看以下内容
http://www.artima.com/cppsource/safebool.html
这就是所谓的安全布尔习语。尽管它在 c++11 中似乎已过时,但它很好地揭示了 sharedPtrs 上 bool 操作的使用。