constexpr I begin() const requires copyable<I>; // (1) C++20
[[nodiscard]] constexpr I begin() requires (!copyable<I>); // (2) C++20
constexpr I begin() requires (!copyable<I>); // (2) C++26
概要
先頭要素を指すイテレータを取得する。
効果
- (1):
return begin_;
- (2):
return std::move(begin_);
ただし、begin_
はsubrange
が内部で保持するイテレータ。
例
#include <ranges>
#include <iostream>
int main()
{
constexpr int a[] = {1, 2, 3};
const std::ranges::subrange sub(a);
// .begin と .end を暗黙的に使用
for (int x : sub) {
std::cout << x << '\n';
}
}
出力
1
2
3
バージョン
言語
- C++20
処理系
- Clang: 13.0.0 ✅
- GCC: 10.1.0 ✅
- ICC: ?
- Visual C++: 2019 Update 10 ✅
参照
- N4861 24 Ranges library
- C++20 ranges
- P2422R1 Remove
nodiscard
annotations from the standard library specification- C++26で
[[nodiscard]]
指定が削除された
- C++26で