Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |memory‎ |unique ptr
       
       
      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*)
      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 operatorbool()constnoexcept;
      (since C++11)
      (constexpr since C++23)

      Checks whether*this owns an object, i.e. whetherget()!=nullptr.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if*this owns an object,false otherwise.

      [edit]Example

      Run this code
      #include <iostream>#include <memory> int main(){std::unique_ptr<int> ptr(newint(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';}

      Output:

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

      [edit]See also

      returns a pointer to the managed object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/operator_bool&oldid=180172"

      [8]ページ先頭

      ©2009-2026 Movatter.jp