Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 2.25 KB

File metadata and controls

62 lines (46 loc) · 2.25 KB

pointer_to

  • memory[meta header]
  • std[meta namespace]
  • pointer_traits[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
static pointer pointer_traits::pointer_to(element_type& r);           // (1) C++11
static constexpr pointer pointer_traits::pointer_to(element_type& r); // (1) C++26


static pointer pointer_traits<T*>::pointer_to(element_type& r) noexcept;           // (2) C++11
static constexpr pointer pointer_traits<T*>::pointer_to(element_type& r) noexcept; // (2) C++20

概要

変数へのポインタを取得する。

戻り値

  • (1) : 型Ptrが静的メンバ関数pointer_to()を持っていればPtr::pointer_to(r)を呼び出してその戻り値を返す。持っていなければ、この関数のインスタンス化は不適格となる。
  • (2) : addressof(r)を返す。

#include <memory>
#include <iostream>

int main()
{
  int value = 3;
  int* p = std::pointer_traits<int*>::pointer_to(value);

  std::cout << *p << std::endl;
}
  • pointer_to[color ff0000]

出力

3

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 [mark verified]
  • GCC: 4.7.3 [mark verified]
  • ICC: ??
  • Visual C++: 2012 [mark verified], 2013 [mark verified]

参照

  • P1006R1 Constexpr in std::pointer_traits
    • C++20で、生ポインタの特殊化(2) pointer_traits<T*>::pointer_toconstexprが付いた(プライマリテンプレート(1)は、reinterpret_castを使うfancy pointerを排除しないため対象外とされた)
  • LWG Issue 3454. pointer_traits::pointer_to should be constexpr
    • C++26で、プライマリテンプレートの(1) pointer_traits::pointer_toにもconstexprが付いた。これによりfancy pointerを使うconstexprコンテナ(SSOを用いたconstexpr std::stringなど)が定数式で動作できるようになる。この変更は欠陥報告 (DR) であり、C++26より前のバージョンでもコンパイラが早期に対応している場合がある