Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::out_ptr

      From cppreference.com
      <cpp‎ |memory‎ |out ptr t
       
       
      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)
       
       
      Defined in header<memory>
      template<class Pointer=void,class Smart,class...Args>
      auto out_ptr( Smart& s, Args&&...args);
      (since C++23)

      Returns anstd::out_ptr_t with deduced template arguments that captures arguments for resetting by reference.

      The program is ill-formed if construction of the return value (see below) is ill-formed.

      Contents

      [edit]Parameters

      s - the object (typically a smart pointer) to adapt
      args... - the arguments for resetting to capture

      [edit]Return value

      std::out_ptr_t<Smart, P, Args&&>(s,std::forward<Args>(args)...), whereP is

      • Pointer, ifPointer is not avoid type. Otherwise,
      • Smart::pointer, if it is valid and denotes a type. Otherwise,
      • Smart::element_type*, ifSmart::element_type is valid and denotes a type. Otherwise,
      • std::pointer_traits<Smart>::element_type*.

      [edit]Notes

      Users may specify the template argument for the template parameterPointer, in order to interoperate with foreign functions that take aPointer*.

      As all arguments for resetting are captured by reference, the returnedout_ptr_t should be a temporary object destroyed at the end of the full-expression containing the call to the foreign function, in order to avoid dangling references.

      Feature-test macroValueStdFeature
      __cpp_lib_out_ptr202106L(C++23)std::out_ptr,std::inout_ptr
      202311L(C++26)freestandingstd::out_ptr andstd::inout_ptr

      [edit]Example

      Usestd::out_ptr to adapt a smart pointer forsqlite3_open, which expects asqlite3** as an out parameter.

      Run this code
      #include <memory>#include <sqlite3.h> int main(){auto close_db=[](sqlite3* db){ sqlite3_close(db);}; {// open an in-memory database, and manage its lifetime with std::unique_ptrstd::unique_ptr<sqlite3, decltype(close_db)> up;        sqlite3_open(":memory:", std::out_ptr(up));         sqlite3* db= up.get();// do something with db ...}{// same as above, but use a std::shared_ptrstd::shared_ptr<sqlite3> sp;        sqlite3_open(":memory:", std::out_ptr(sp, close_db));         sqlite3* db= sp.get();// do something with db ...}}

      [edit]See also

      (C++23)
      creates aninout_ptr_t with an associated smart pointer and resetting arguments
      (function template)[edit]
      creates a unique pointer that manages a new object
      (function template)[edit]
      creates a shared pointer that manages a new object
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/out_ptr_t/out_ptr&oldid=182903"

      [8]ページ先頭

      ©2009-2025 Movatter.jp