GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::ranges::data - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. ranges
  3. data

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

customization point object
<ranges>

std::ranges::data(C++20)

namespace std::ranges {
  inline namespace /*unspecified*/ {
    inline constexpr /*unspecified*/ data = /*unspecified*/;
  }
}

概要

Rangeの要素が格納されたメモリ領域へのポインタを取得する関数オブジェクト。

効果

部分式Eの型をT、tをEを評価した値とする。このとき、式ranges::data(E)の効果は以下の通り。

  1. Eがrvalueかつenable_borrowed_range<remove_cv_t<T>>がfalseであれば、呼び出しは不適格。
  2. Tが配列型かつremove_all_extents_t<T>が不完全型であれば、呼び出しは不適格(診断不要)。
  3. auto(t.data())が有効な式でその型がオブジェクトへのポインタであれば、auto(t.data())と等しい。
  4. ranges::begin(t)が有効な式で、その型がcontiguous_iteratorのモデルであれば、to_address(ranges::begin(E))と等しい。

どれにも当てはまらないとき、呼び出しは不適格。

戻り値

Rangeの要素が格納されたメモリ領域へのポインタ。

カスタマイゼーションポイント

3か4の条件を満たすようにする。例えば、ユーザー定義のクラスにメンバ関数dataを持たせるか、ranges::beginを通じてカスタマイズできる。

備考

ranges::data(E)が有効な式であるとき、その型はオブジェクトへのポインタである。

例

#include <vector>
#include <iostream>
#include <ranges>

void some_c_like_api_function(const int* arr, std::size_t arr_size)
{
  std::cout << "array size:" << arr_size << " at " << static_cast<const void*>(arr) << std::endl;
}

int main()
{
  int arr[4] = {};
  some_c_like_api_function(std::ranges::data(arr), std::ranges::size(arr));
  std::vector<int> v { 12 };
  some_c_like_api_function(std::ranges::data(v), std::ranges::size(v));
}

出力例

array size:4 at 0x7fff28ab1c50
array size:1 at 0x5596494f02b0

バージョン

言語

  • C++20

処理系

  • Clang: 13.0.0 ✅
  • GCC: 10.1.0 ✅
  • Visual C++: 2019 Update 10 ✅

関連項目

  • std::data
  • C++23 auto(x)による一時オブジェクトへの変換

参照

  • N4861 24 Ranges library
  • C++20 ranges
  • P0849R8 auto(x): decay-copy in the language
    • C++23で、コピーを表す説明専用のdecay-copyが、言語機能のauto(x)へ置き換えられた
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.