From cppreference.com
template<classPointer=void,classSmart,class...Args>autoout_ptr(Smart&s,Args&&...args);(since C++23)
(constexpr since C++26)Returns an
with deduced template arguments that captures arguments for resetting by reference.
If construction of the return value (see below), the program is ill-formed.
Parameters
s - the object (typically a smart pointer) to adapt args - the arguments for resetting to capture Return value
std::out_ptr_t<Smart,P,Args&&>(s,std::forward<Args>(args)...), where P is
Pointer, if Pointer is not same as void. Otherwise,
Smart::pointer, if it is valid and denotes a type. Otherwise,
Smart::element_type*, if Smart::element_type is valid and denotes a type. Otherwise,
std::pointer_traits<Smart>::element_type*.
Notes
Users may specify the template argument for the template parameter Pointer, in order to interoperate with foreign functions that take a Pointer*.
As all arguments for resetting are captured by reference, the returned out_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.
macro ValueStdFeature
(C++23)std::out_ptr, std::inout_ptr
(C++26)freestanding std::out_ptr and std::inout_ptr
(C++26)Constexpr std::out_ptrExample
Uses std::out_ptr to adapt a smart pointer for
, which expects an sqlite3** as an out parameter.
#include<memory>#include<sqlite3.h>intmain(){autoclose_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 ...}}See also