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

履歴 編集

function template
<iterator>

std::data(C++17)

namespace std {
  template <class C>
  constexpr
    auto data(C& c)
      -> decltype(c.data());       // (1) C++17
  template <class C>
  constexpr auto data(C& c)
    noexcept(noexcept(c.data()))
    -> decltype(c.data());         // (1) C++26

  template <class C>
  constexpr auto
    data(const C& c)
      -> decltype(c.data());       // (2) C++17
  template <class C>
  constexpr auto data(const C& c)
    noexcept(noexcept(c.data()))
    -> decltype(c.data());         // (2) C++26

  template <class T, std::size_t N>
  constexpr T*
    data(T (&array)[N]) noexcept;  // (3) C++17

  template <class E>
  constexpr const E*
    data(initializer_list<E> il) noexcept;  // (4) C++17、C++26で削除
}

(4)はC++26で削除された。std::initializer_listに、メンバ関数版のdata()が追加されたため、このオーバーフローは不要になった。使い方としてはこれまで通りに使用できる。

概要

コンテナの要素が格納されたメモリ領域へのポインタを取得する。

戻り値

  • (1), (2) : return c.data();
  • (3) : return array;
  • (4) : return il.begin();

備考

#include <vector>
#include <iostream>

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::data(arr), std::size(arr));
  std::vector<int> v { 12 };
  some_c_like_api_function(std::data(v), std::size(v));
}

出力例

array size:4 at 0x7fff0833f980
array size:1 at 0x22e42b0

バージョン

言語

  • C++17

処理系

参照