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

履歴 編集

function
<ranges>

std::ranges::elements_view::base(C++20)

constexpr V base() const &
  requires copy_constructible<V>;  // (1) C++20

constexpr V base() &&;             // (2) C++20

概要

メンバ変数として保持している、元のviewを取得する。

効果

  • (1) : return base_;
  • (2) : return std::move(base_);

#include <ranges>
#include <map>
#include <string>
#include <concepts>
#include <iostream>

int main() {
  std::map<int, std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}};

  std::ranges::keys_view kv(m);

  // (1) コピーして取得
  auto base1 = kv.base();
  static_assert(std::same_as<decltype(base1), std::ranges::ref_view<std::map<int, std::string>>>);

  // (2) ムーブして取得
  auto base2 = std::move(kv).base();
  static_assert(std::same_as<decltype(base2), std::ranges::ref_view<std::map<int, std::string>>>);

  // 取得したviewを使用(元のmap要素を表示)
  for (const auto& [key, value] : base1) {
    std::cout << key << ": " << value << std::endl;
  }
}

出力

1: one
2: two
3: three

バージョン

言語

  • C++20

処理系

参照