std::inout_ptr_t(3)
π std::inout_ptr_t π
okay so bestie, this one lives in the C++ Standard Library and honestly? it's giving "glue code that slays". let's get into it β¨
πΈ NAME πΈ
std::inout_ptr_t β literally std::inout_ptr_t. that's the name. she's self-titled. main character energy π
π¦ Synopsis π¦
so first things first, she's Defined in header <memory> β go put that at the top of your file, no cap.
template< class Smart, class Pointer, class... Args > (since C++23)
class inout_ptr_t;
okay so what does she do? inout_ptr_t is used to adapt types such as smart pointers for foreign functions that reset ownership via a Pointer* (usually T** for some object type T) or void** parameter. like, she's the translator at the function-call border, iykyk π
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 adapted Smart object, and finally resets the adapted Smart object with the result and the captured arguments when it is destroyed. she does the whole little routine. obsessed. π
inout_ptr_t behaves as if it holds following 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 while destruction, and - a member subobject that suitable for storing a
Pointerwithin it and providing avoid*object, where thePointerorvoid*object is generally exposed to a foreign function for ownership resetting.
If Smart is not a pointer type, release() is called at most once on the adapted object. Implementations may call release() within constructor, or before resetting within destructor if the Pointer value is not null. (yes, "at most once" β that's the deal, don't push her π)
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. you literally just pick. it's very choose-your-own-adventure π
π§ 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 accesses for ownership resettingArgs...β type of captured arguments used for resetting the adapted object
β Type requirements β
Pointermust meet the requirements of NullablePointer.- The program is ill-formed if
Smartis astd::shared_ptrspecialization. yeah. forbidden. we'll get to why π
π«Ά Specializations π«Ά
Unlike most class templates in the standard library, program-defined specializations of inout_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. cute little hall pass, honestly π
πΈ Member functions πΈ
- constructor β constructs an
inout_ptr_t(C++23) (public member function) operator=βinout_ptr_tis not assignable[deleted](C++23) (public member function)- destructor β resets the adapted smart pointer after releasing its ownership (C++23) (public member function)
operator Pointer*,operator void**β converts theinout_ptr_tto the address of the storage for output (public member function) (C++23)
π Non-member functions π
inout_ptrβ creates aninout_ptr_twith an associated smart pointer and resetting arguments (C++23) (function template)
π Notes π
inout_ptr_t expects that the foreign functions release the ownership represented by the value of the pointed-to Pointer, and then re-initialize it. As such operation requires unique ownership, the usage with std::shared_ptr is forbidden. (that's the why for the ill-formed thing above, bestie β shared ownership and "release then re-init" simply do not vibe together π)
The typical usage of inout_ptr_t is creating its temporary objects by std::inout_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_resetter(T**); and std::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;
look at her go. one line vs five. she ate π₯
β οΈ It is not recommended to create an inout_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. seriously. keep her on the stack, automatic storage duration only, or you're gonna have a bad time π
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 time, periodt:
__cpp_lib_out_ptrβ value202106L, std(C++23), featurestd::out_ptr, std::inout_ptr__cpp_lib_out_ptrβ value202311L, std(C++26), featurefreestanding std::out_ptr and std::inout_ptr
π Example π
This section is incomplete
Reason: no example
(she's a work in progress, we love her anyway π«Ά)
π See also π
out_ptr_t(C++23) β interoperates with foreign pointer setters and resets a smart pointer on destruction (class template)unique_ptr(C++11) β smart pointer with unique object ownership semantics (class template)shared_ptr(C++11) β smart pointer with shared object ownership semantics (class template)
π Category π
- Todo no example
okay that's the whole page, bestie, nothing skipped, nothing invented π sign-off time: this one came from http://cppreference.com and the version tag is 2024.06.10, so cite it cute when you reference it. luv u, go write some inout_ptr code that slays π
β¨
π¦ 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