Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::pointer_traits

      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)
       
      std::pointer_traits
      Member functions
       
      Defined in header<memory>
      template<class Ptr>
      struct pointer_traits;
      (1)(since C++11)
      template<class T>
      struct pointer_traits<T*>;
      (2)(since C++11)

      Thepointer_traits class template provides the standardized way to access certain properties of pointer-like types (fancy pointers, such asboost::interprocess::offset_ptr). The standard templatestd::allocator_traits relies onpointer_traits to determine the defaults for various typedefs required byAllocator.

      1) The non-specializedpointer_traits conditionally declares the following members:

      Let/*element-type-of*/<Ptr> be

      • Ptr::element_type if present;
      • otherwise,T ifPtr is a class template specializationTemplate<T, Args...>, whereArgs... is zero or more type arguments;
      • otherwise, not defined.

      If/*element-type-of*/<Ptr> is not defined, the primary template has no members specified in this page.

      Contents

      [edit]Member types

      Type Definition
      pointerPtr
      element_type/*element-type-of*/<Ptr>
      difference_typePtr::difference_type if present, otherwisestd::ptrdiff_t

      [edit]Member alias templates

      Template Definition
      template<class U>using rebindPtr::rebind<U> if exists, otherwiseTemplate<U, Args...> ifPtr is a template specializationTemplate<T, Args...>

      [edit]Member functions

      [static]
      obtains a dereferenceable pointer to its argument
      (public static member function)[edit]
      2) A specialization is provided for pointer types,T*, which declares the following members:

      [edit]Member types

      Type Definition
      pointerT*
      element_typeT
      difference_typestd::ptrdiff_t

      [edit]Member alias templates

      Template Definition
      template<class U>using rebindU*

      [edit]Member functions

      [static]
      obtains a dereferenceable pointer to its argument
      (public static member function)[edit]

      [edit]Optional member functions of program-defined specializations

      [static](C++20)(optional)
      obtains a raw pointer from a fancy pointer (inverse ofpointer_to)
      (public static member function)[edit]

      [edit]Notes

      The rebind member template alias makes it possible, given a pointer-like type that points toT, to obtain the same pointer-like type that points toU. For example,

      using another_pointer= std::pointer_traits<std::shared_ptr<int>>::rebind<double>;static_assert(std::is_same<another_pointer,std::shared_ptr<double>>::value);

      A specialization for user-defined fancy pointer types may provide an additional static member functionto_address to customize the behavior ofstd::to_address.

      (since C++20)
      Feature-test macroValueStdFeature
      __cpp_lib_constexpr_memory201811L(C++20)constexpr instd::pointer_traits

      [edit]Example

      Run this code
      #include <iostream>#include <memory> template<class Ptr>struct BlockList{// Predefine a memory blockstruct block; // Define a pointer to a memory block from the kind of pointer Ptr s// If Ptr is any kind of T*, block_ptr_t is block*// If Ptr is smart_ptr<T>, block_ptr_t is smart_ptr<block>using block_ptr_t=typename std::pointer_traits<Ptr>::template rebind<block>; struct block{std::size_t size{};        block_ptr_t next_block{};};     block_ptr_t free_blocks;}; int main(){[[maybe_unused]]    BlockList<int*> bl1;// The type of bl1.free_blocks is BlockList<int*>:: block*     BlockList<std::shared_ptr<char>> bl2;// The type of bl2.free_blocks is// std::shared_ptr<BlockList<std::shared_ptr<char>>::block>std::cout<< bl2.free_blocks.use_count()<<'\n';}

      Output:

      ​0​

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3545C++11primary template caused hard error whenelement_type is invalidmade SFINAE-friendly

      [edit]See also

      provides information about allocator types
      (class template)[edit]
      (C++11)
      obtains actual address of an object, even if the& operator is overloaded
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/pointer_traits&oldid=160243"

      [8]ページ先頭

      ©2009-2025 Movatter.jp