Namespaces
Variants

std::unique_ptr<T,Deleter>:: operator bool

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
explicit operator bool ( ) const noexcept ;
(自 C++11 起)
(自 C++23 起为 constexpr)

检查 * this 是否拥有对象,即 get() != nullptr 是否成立。

目录

参数

(无)

返回值

true * this 拥有对象,否则为 false

示例

#include <iostream>
#include <memory>
int main()
{
    std::unique_ptr<int> ptr(new int(42));
    if (ptr)
        std::cout << "before reset, ptr is: " << *ptr << '\n';
    ptr.reset();
    (ptr ? (std::cout << "after reset, ptr is: " << *ptr)
        : (std::cout << "after reset ptr is empty")) << '\n';
}

输出:

before reset, ptr is: 42
after reset ptr is empty

参见

返回指向被管理对象的指针
(公开成员函数)