Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::inout_ptr_t

      From cppreference.com
      <cpp‎ |memory
       
       
      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 Smart,class Pointer,class...Args>
      class inout_ptr_t;
      (since C++23)

      inout_ptr_t is used to adapt types such as smart pointers for foreign functions that reset ownership via aPointer* (usuallyT** for some object typeT) orvoid** parameter.

      inout_ptr_t captures additional arguments on construction, provides a storage for the result which such an aforementioned foreign function accesses, releases the ownership held by the adaptedSmart object, and finally resets the adaptedSmart object with the result and the captured arguments when it is destroyed.

      inout_ptr_t behaves as if it holds following non-static data members:

      • aSmart& reference, which is bound to the adapted object on construction,
      • for everyT inArgs..., a member of typeT, which is an argument captured on construction and used for resetting while destruction, and
      • a member subobject that suitable for storing aPointer within it and providing avoid* object, where thePointer orvoid* object is generally exposed to a foreign function for ownership resetting.

      IfSmart is not a pointer type,release() is called at most once on the adapted object. Implementations may callrelease() within constructor, or before resetting within destructor if thePointer value is not null.

      Users can control whether each argument for resetting is captured by copy or by reference, by specifying an object type or a reference type inArgs... respectively.

      Contents

      [edit]Template parameters

      Smart - the type of the object (typically a smart pointer) to adapt
      Pointer - type of the object (typically a raw pointer) to which a foreign function accesses for ownership resetting
      Args... - type of captured arguments used for resetting the adapted object
      Type requirements
      -
      Pointer must meet the requirements ofNullablePointer.
      -
      The program is ill-formed ifSmart is astd::shared_ptr specialization.

      [edit]Specializations

      Unlike most class templates in the standard library,program-defined specializations ofinout_ptr_t that depend on at least oneprogram-defined type need not meet the requirements for the primary template.

      This license allows a program-defined specialization to expose the raw pointer stored within a non-standard smart pointer to foreign functions.

      [edit]Member functions

      constructs aninout_ptr_t
      (public member function)
      operator=
      [deleted](C++23)
      inout_ptr_t is not assignable
      (public member function)
      resets the adapted smart pointer after releasing its ownership
      (public member function)
      converts theinout_ptr_t to the address of the storage for output
      (public member function)

      [edit]Non-member functions

      (C++23)
      creates aninout_ptr_t with an associated smart pointer and resetting arguments
      (function template)[edit]

      [edit]Notes

      inout_ptr_t expects that the foreign functions release the ownership represented by the value of the pointed-toPointer, and then re-initialize it. As such operation requires unique ownership, the usage withstd::shared_ptr is forbidden.

      The typical usage ofinout_ptr_t is creating its temporary objects bystd::inout_ptr, which resets the adapted smart pointer immediately. E.g. given a setter function and a smart pointer of appropriate type declared withint foreign_resetter(T**); andstd::unique_ptr<T, D> up; respectively,

      if(int ec= foreign_resetter(std::inout_ptr(up)))return ec;

      is roughly equivalent to

      T*raw_p= up.get();up.release();int ec= foreign_resetter(&raw_p);up.reset(raw_p);if(ec!=0)return ec;

      It is not recommended to create aninout_ptr_t object of astorage duration other than automatic storage duration, because such code is likely to produce dangling references and result in undefined behavior on destruction.

      Captured arguments are typically packed into astd::tuple<Args...>. Implementations may use different mechanism to provide thePointer orvoid* object they need hold.


      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

      This section is incomplete
      Reason: no example

      [edit]See also

      (C++23)
      interoperates with foreign pointer setters and resets a smart pointer on destruction
      (class template)[edit]
      (C++11)
      smart pointer with unique object ownership semantics
      (class template)[edit]
      (C++11)
      smart pointer with shared object ownership semantics
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/inout_ptr_t&oldid=170143"

      [8]ページ先頭

      ©2009-2025 Movatter.jp