Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::shared...operatorbool(3) C++ Standard Libarystd::shared...operatorbool(3)

NAME
       std::shared_ptr::operatorbool - std::shared_ptr::operatorbool

Synopsis
	  explicit operator bool() const noexcept;

	  Checks  if  *this  stores  a non-null	pointer, i.e. whether get() !=
       nullptr.

Parameters
	  (none)

Return value
	  true if *this	stores a pointer, false	otherwise.

Notes
	  An empty shared_ptr (where use_count() == 0) may  store  a  non-null
       pointer accessible
	  by get(), e.g. if it were created using the aliasing constructor.

Example
       // Run this code

	#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);
	}

Output:
	ptr is not a valid pointer.
	*ptr=7

See also
	  get returns the stored pointer
	      (public member function)

http://cppreference.com		  2022.07.31	 std::shared...operatorbool(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::shared_ptr::operatorbool&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help