std::shared_ptr::operator bool
explicit operator bool() const; | | |
|---|
检查*this存储非空指针,即是否为get() != nullptr...
参数
%280%29
返回值
true如果*this存储一个指针,false否则。
例外
noexcept规格:
noexcept
注记
空共享[医]PTR%28use_count() == 0%5月29日存储一个非空指针。get(),例如,如果它是使用别名构造函数创建的。
例
二次
#include <iostream>
#include <memory>
typedef std::shared_ptr<int> IntPtr;
void report(IntPtr ptr)
{
if (ptr) {
std::cout << "*ptr=" << *ptr << "\n";
} else {
std::cout << "ptr is not a valid pointer.\n";
}
}
int main()
{
IntPtr ptr;
report(ptr);
ptr = IntPtr(new int(7));
report(ptr);
}二次
产出:
二次
ptr is not a valid pointer.
*ptr=7二次
另见
get | returns the stored pointer (public member function) |
|---|
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

