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

履歴 編集

class template
<ranges>

std::ranges::as_rvalue_view(C++23)

namespace std::ranges {
  template<view V>
    requires (input_range<V>)
  class as_rvalue_view : public view_interface<as_rvalue_view<V>> { …… }; // (1)

  namespace views {
    inline constexpr /*unspecified*/ as_rvalue = /*unspecified*/;      // (2)
  }
}

概要

  • (1): 要素をrvalueにするview
  • (2): as_rvalue_viewまたは同じ効果を実現するviewを生成するRangeアダプタオブジェクト

このviewは、あるRangeの要素をムーブして別のコンテナに挿入する場合などに利用できる。

vector<string> words = {"the", "quick", "brown", "fox", "ate", "a", "pterodactyl"};
vector<string> new_words;
ranges::copy(words | views::as_rvalue, back_inserter(new_words));

Rangeコンセプト

borrowed sized output input forward bidirectional random_access contiguous common viewable view

※ 参照先のRangeに従う

テンプレートパラメータ制約

効果

  • (2): 式views::as_rvalue(E)はRangeアダプタオブジェクトを表し、その効果は次の通り

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++20
base Rの参照を取得する C++20
begin 先頭を指すイテレータを取得する C++20
end 番兵を取得する C++20
size 要素数を取得する C++20

rを参照先のRangeとする。sizeは、ranges::size(r)が有効な式であるときに定義される。

継承しているメンバ関数

名前 説明 対応バージョン
empty Rangeが空かどうかを判定する C++20
operator bool Rangeが空でないかどうかを判定する C++20
data Rangeの先頭へのポインタを取得する C++20
front 先頭要素への参照を取得する C++20
back 末尾要素への参照を取得する C++20
operator[] 要素へアクセスする C++20
cbegin 定数イテレータを取得する C++23
cend 定数イテレータ(番兵)を取得する C++23

推論補助

名前 説明 対応バージョン
(deduction_guide) クラステンプレートの推論補助 C++20

#include <ranges>
#include <vector>
#include <iterator>
#include <print>

int main() {
  using namespace std;

  vector<string> words = {"the", "quick", "brown", "fox", "ate", "a", "pterodactyl"};
  vector<string> new_words;
  ranges::copy(words | views::as_rvalue, back_inserter(new_words));
  print("{}", new_words);
}

出力

["the", "quick", "brown", "fox", "ate", "a", "pterodactyl"]

実装例

namespace std::ranges {
  template<view V>
    requires input_range<V>
  class as_rvalue_view : public view_interface<as_rvalue_view<V>> {
    V base_ = V();      // exposition only

  public:
    as_rvalue_view() requires default_initializable<V> = default;
    constexpr explicit as_rvalue_view(V base);

    constexpr V base() const & requires copy_constructible<V> { return base_; }
    constexpr V base() && { return std::move(base_); }

    constexpr auto begin() requires (!simple-view<V>) {
      return move_iterator(ranges::begin(base_));
    }

    constexpr auto begin() const requires range<const V> {
      return move_iterator(ranges::begin(base_));
    }

    constexpr auto end() requires (!simple-view<V>) {
      if constexpr (common_range<V>) {
        return move_iterator(ranges::end(base_));
      } else {
        return move_sentinel(ranges::end(base_));
      }
    }

    constexpr auto end() const requires range<const V> {
      if constexpr (common_range<const V>) {
        return move_iterator(ranges::end(base_));
      } else {
        return move_sentinel(ranges::end(base_));
      }
    }

    constexpr auto size() requires sized_range<V> { return ranges::size(base_); }
    constexpr auto size() const requires sized_range<const V> { return ranges::size(base_); }
  };

  template<class R>
    as_rvalue_view(R&&) -> as_rvalue_view<views::all_t<R>>;
}

constexpr explicit as_rvalue_view(V base);

バージョン

言語

  • C++23

処理系

参照