std::out_ptr_t(3)
π¦ std::out_ptr_t(3) β C++ Standard Libary π¦
π NAME π
std::out_ptr_t β std::out_ptr_t
β¨ SYNOPSIS β¨
Defined in header <memory>
template< class Smart, class Pointer, class... Args > (since C++23)
class out_ptr_t;
okay bestie, so out_ptr_t is here to adapt types like smart pointers for foreign functions that output their results through a Pointer* parameter (usually T** for some object type T) or a void** parameter. it's giving "bridge between my cute smart pointer and this raw C API" energy.
out_ptr_t captures extra arguments on construction, provides storage for the result that the foreign function writes into, and finally resets the adapted Smart object with that result plus the captured arguments when it gets destroyed. iconic cleanup behavior, honestly.
out_ptr_t behaves as if it holds these non-static data members:
- a
Smart&reference, which is bound to the adapted object on construction, - for every
TinArgs..., a member of typeT, which is an argument captured on construction and used for resetting during destruction, and - a member subobject suitable for storing a
Pointerwithin it and providing avoid*object, where thePointerorvoid*object is generally exposed to a foreign function for re-initialization.
Users can control whether each argument for resetting is captured by copy or by reference by specifying an object type or a reference type in Args..., respectively. cute little control knob, no cap.
π§ TEMPLATE PARAMETERS π§
Smartβ the type of the object (typically a smart pointer) to adaptPointerβ type of the object (typically a raw pointer) to which a foreign function writes its resultArgs...β type of captured arguments used for resetting the adapted object
π‘οΈ TYPE REQUIREMENTS π‘οΈ
Pointermust meet the requirements ofNullablePointer.- The program is ill-formed if
Smartis astd::shared_ptrspecialization andsizeof...(Args) == 0.
π SPECIALIZATIONS π
Unlike most class templates in the standard library, program-defined specializations of out_ptr_t that depend on at least one program-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. slay for custom smart pointer girlies π
π MEMBER FUNCTIONS π
constructorβ constructs anout_ptr_t(C++23) (public member function)operator=βout_ptr_tis not assignable[deleted](C++23) (public member function)destructorβ resets the adapted smart pointer (C++23) (public member function)operator Pointer*β converts theout_ptr_tto the address of the storage for output (public member function)operator void**β (public member function) (C++23)
π«Ά NON-MEMBER FUNCTIONS π«Ά
out_ptrβ creates anout_ptr_twith an associated smart pointer and resetting arguments (C++23) (function template)
π NOTES π
out_ptr_t expects that the foreign functions do not use the value of the pointed-to Pointer, and only re-initialize it. The value of the smart pointer before adaption is not used. so don't expect it to peek at the old pointer, bestie.
The typical usage of out_ptr_t is creating its temporary objects by std::out_ptr, which resets the adapted smart pointer immediately. E.g. given a setter function and a smart pointer of appropriate type declared with int foreign_setter(T**); and std::unique_ptr<T, D> up; respectively,
int foreign_setter(T**);
std::unique_ptr<T, D> up;
if (int ec = foreign_setter(std::out_ptr(up)))
return ec;
is roughly equivalent to
int foreign_setter(T**);
std::unique_ptr<T, D> up;
T* raw_p{};
int ec = foreign_setter(&raw_p);
up.reset(raw_p);
if (ec != 0)
return ec;
It is not recommended to create an out_ptr_t object of a storage duration other than automatic storage duration, because such code is likely to produce dangling references and result in undefined behavior on destruction. bestie please keep it automatic, this is not the vibe for dangling references π
out_ptr_t forbids the usage that would reset a std::shared_ptr without specifying a deleter, because it would call std::shared_ptr::reset and replace a custom deleter later.
Captured arguments are typically packed into a std::tuple<Args...>. Implementations may use different mechanism to provide the Pointer or void* object they need hold.
Feature-test macro tea β:
__cpp_lib_out_ptrβ202106Lβ(C++23)βstd::out_ptr,std::inout_ptr__cpp_lib_out_ptrβ202311Lβ(C++26)β freestandingstd::out_ptrandstd::inout_ptr
π EXAMPLE π
This section is incomplete. Reason: no example. yes bestie, the example section is literally empty, we love a mysterious icon π
π SEE ALSO π
inout_ptr_tβ interoperates with foreign pointer setters, obtains the initial pointer value from a smart pointer, and resets it on destruction (C++23) (class template)unique_ptrβ smart pointer with unique object ownership semantics (C++11) (class template)shared_ptrβ smart pointer with shared object ownership semantics (C++11) (class template)
π·οΈ CATEGORY π·οΈ
- Todo no example
mwah π β from http://cppreference.com, dated 2024.06.10, for std::out_ptr_t(3) π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms