std::unique_ptr<T,Deleter>::operator bool
explicit operator bool() const noexcept; | (с C++11) (constexpr с C++23) |
Проверяет, владеет ли *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
См. также
| возвращает указатель на управляемый объект (публичный член-функция) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/memory/unique_ptr/operator_bool