• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    class template
    <memory>

    std::pointer_traits

    namespace std {
      template <class Ptr>
      struct pointer_traits;
    
      template <class T>
      struct pointer_traits<T*>;
    }
    

    概要

    pointer_traitsは、ポインタと見なせる型の情報に統一的にアクセスするためのクラスである。スマートポインタもポインタと見なせる。

    静的メンバ関数

    名前 説明 対応バージョン
    pointer_to 変数へのポインタを取得する C++11

    メンバ型

    名前 説明 対応バージョン
    pointer ポインタと見なせる型 Ptr C++11
    element_type ポインタが指す要素型。
    Ptrelement_type型を持っていればそれを使用する。型Ptrが要素型Tと0個以上の他のパラメータをとるクラステンプレートであればTを使用する。そうでなければ不適格となる。
    C++11
    difference_type ポインタの差を表す符号付き整数型。
    Ptrdifference_type型を持っていればそれを使用し、そうでなければptrdiff_t型を使用する。
    C++11
    rebind<U> 型の再束縛。
    Ptrrebind<U>を持っていればそれを使用する。型Ptrが型Tと0個以上の他のパラメータをとるクラステンプレートであれば、型Uで再束縛したPtr型を使用する。どちらもなければ、rebind<U>のインスタンス化は不適格となる。
    C++11

    メンバ型(ポインタに対する特殊化)

    名前 説明 対応バージョン
    pointer T* C++11
    element_type T C++11
    difference_type ptrdiff_t C++11
    rebind<U> U* C++11

    #include <memory>
    #include <type_traits>
    
    int main()
    {
      // スマートポインタの要素型を取得する
      using smart_ptr_element = std::pointer_traits<std::shared_ptr<int>>::element_type;
      static_assert(std::is_same<smart_ptr_element, int>::value, "element type is int");
    
      // ポインタの要素型を取得する
      using ptr_element = std::pointer_traits<int*>::element_type;
      static_assert(std::is_same<ptr_element, int>::value, "element type is int");
    }
    

    出力

    バージョン

    言語

    • C++11

    処理系

    参照