std::to_address(3)
π std::to_address(3) β C++ Standard Libary π
okay bestie, welcome to the <memory>-core page for std::to_address β¨ itβs giving βget the address without forming a referenceβ and weβre gonna get into every single detail, no cap π
πΈ NAME πΈ
std::to_address β std::to_address (yes literally, thatβs the whole name ππ
)
β¨ Synopsis β¨
Defined in header <memory>
template< class Ptr > (1) (since C++20)
constexpr auto to_address( const Ptr& p ) noexcept;
template< class T > (2) (since C++20)
constexpr T* to_address( T* p ) noexcept;
Obtain the address represented by p without forming a reference to the object pointed to by p.
- Fancy pointer overload: If the expression
std::pointer_traits<Ptr>::to_address(p)is well-formed, returns the result of that expression. Otherwise, returnsstd::to_address(p.operator->()). - Raw pointer overload: If
Tis a function type, the program is ill-formed. Otherwise, returnspunmodified.
π Parameters π
pβ fancy or raw pointer
π¦ Return value π¦
Raw pointer that represents the same address as p does.
β¨ Possible implementation β¨
template<class T>
constexpr T* to_address(T* p) noexcept
{
static_assert(!std::is_function_v<T>);
return p;
}
template<class T>
constexpr auto to_address(const T& p) noexcept
{
if constexpr (requires{ std::pointer_traits<T>::to_address(p); })
return std::pointer_traits<T>::to_address(p);
else
return std::to_address(p.operator->());
}
π Notes π
std::to_addresscan be used even whenpdoes not reference storage that has an object constructed in it, in which casestd::addressof(*p)cannot be used because there is no valid object for the parameter ofstd::addressofto bind to.- The fancy pointer overload of
std::to_addressinspects thestd::pointer_traits<Ptr>specialization. If instantiating that specialization is itself ill-formed (typically becauseelement_typecannot be defined), that results in a hard error outside the immediate context and renders the program ill-formed. std::to_addressmay additionally be used on iterators that satisfystd::contiguous_iterator.
π Feature-test macro π
__cpp_lib_to_addressβ value201711L, std(C++20), feature: Utility to convert a pointer to a raw pointer (std::to_address)
π Example π
// Run this code
#include <memory>
template<class A>
auto allocator_new(A& a)
{
auto p = a.allocate(1);
try
{
std::allocator_traits<A>::construct(a, std::to_address(p));
}
catch (...)
{
a.deallocate(p, 1);
throw;
}
return p;
}
template<class A>
void allocator_delete(A& a, typename std::allocator_traits<A>::pointer
p)
{
std::allocator_traits<A>::destroy(a, std::to_address(p));
a.deallocate(p, 1);
}
int main()
{
std::allocator<int> a;
auto p = allocator_new(a);
allocator_delete(a, p);
}
π See also π
pointer_traitsβ provides information about pointer-like types;(C++11), class template.to_addressβ obtains a raw pointer from a fancy pointer (inverse ofpointer_to);[static],(C++20)(optional), public static member function ofstd::pointer_traits<Ptr>.
okay bye bestie π this page is from http://cppreference.com, dated 2024.06.10, and itβs the std::to_address(3) manual β slay and stay pointer-safe β¨π«Ά
π¦ 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