Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::to_address

      From cppreference.com
      <cpp‎ |memory
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
      Defined in header<memory>
      template<class Ptr>
      constexprauto 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 byp without forming a reference to the object pointed to byp.

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

      Contents

      [edit]Parameters

      p - fancy or raw pointer

      [edit]Return value

      Raw pointer that represents the same address asp does.

      [edit]Possible implementation

      template<class T>constexpr T* to_address(T* p)noexcept{    static_assert(!std::is_function_v<T>);return p;} template<class T>constexprauto to_address(const T& p)noexcept{ifconstexpr(requires{std::pointer_traits<T>::to_address(p);})returnstd::pointer_traits<T>::to_address(p);elsereturn std::to_address(p.operator->());}

      [edit]Notes

      std::to_address can be used even whenp does 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::addressof to bind to.

      The fancy pointer overload ofstd::to_address inspects thestd::pointer_traits<Ptr> specialization. If instantiating that specialization is itself ill-formed (typically becauseelement_type cannot be defined), that results in a hard error outside the immediate context and renders the program ill-formed.

      std::to_address may additionally be used on iterators that satisfystd::contiguous_iterator.

      Feature-test macroValueStdFeature
      __cpp_lib_to_address201711L(C++20)Utility to convert a pointer to a raw pointer (std::to_address)

      [edit]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,typenamestd::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);}

      [edit]See also

      provides information about pointer-like types
      (class template)[edit]
      [static](C++20)(optional)
      obtains a raw pointer from a fancy pointer (inverse ofpointer_to)
      (public static member function ofstd::pointer_traits<Ptr>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/to_address&oldid=154688"

      [8]ページ先頭

      ©2009-2025 Movatter.jp