[ Web Proxy ]
URL:
Viewing: http://ja.cppreference.com/cpp/memory/shared_ptr/operator_bool [Back]  [Original]

std::shared_ptr<T>::operator bool - cppreference.com
cppreference.com

std::shared_ptr<T>::operator bool

: cppreference.com
 
C++
(C++20)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
 
 
 
 
<tbody> </tbody>
explicit operator bool() const noexcept;

*this get() != nullptr

()

*this true false

(use_count() == 0) shared_ptr get() shared_ptr

#include <iostream>
#include <memory>

void report(std::shared_ptr<int> ptr) 
{
    if (ptr) {
        std::cout << "*ptr=" << *ptr << "\n";
    } else {
        std::cout << "ptr is not a valid pointer.\n";
    }
}

int main()
{
    std::shared_ptr<int> ptr;
    report(ptr);

    ptr = std::make_shared<int>(7);
    report(ptr);
}

:

ptr is not a valid pointer.
*ptr=7


() [edit]

Web Proxy Viewer  |  New URL  |  Original Page