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
処理系
- Clang: 13.0.0 ✅
- GCC: 10.1.0 ✅
- ICC: ?
- Visual C++: 2019 Update 10 ✅