Namespaces
Variants
Views
Actions

std::to_address

From cppreference.com
< cpp‎ | memory
 
 
 
Dynamic memory management
Uninitialized storage
(C++17)
(deprecated since c++17)
(deprecated since c++17)
(deprecated since c++17)
Garbage collection support
Miscellaneous
to_address
(C++20)
(C++11)
(C++11)
C Library
Low level memory management
 
Defined in header <memory>
template< class Ptr >
auto to_address(const Ptr& p) noexcept;
(1) (since C++20)
template< class T >
constexpr T* to_address(T* p) noexcept;
(2) (since C++20)

Obtain the address represented by p without forming a reference to the pointee.

1) Fancy pointer overload: If the expression std::pointer_traits<Ptr>::to_address(p) is well-formed, returns the result of that expression. Otherwise, returns std::to_address(p.operator->()).
2) Raw pointer overload: If T is a function type, the program is ill-formed. Otherwise, returns p unmodified.

Contents

[edit] Parameters

p - fancy or raw pointer

[edit] Return value

Raw pointer that represents the same address as p does.

[edit] Notes

std::to_address can be used even when p does not reference storage that has an object constructed in it, in which case std::addressof(*p) cannot be used because there's no valid object for the parameter of std::addressof to bind to.

[edit] Example

[edit] See also

provides information about pointer-like types
(class template) [edit]
[static] (C++20)
obtains a raw pointer from a fancy pointer (inverse of pointer_to)
(public static member function of std::pointer_traits) [edit]