Guideline for how to declare variables in a range-based for loop
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
- issue の種類
- ドキュメント
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- cpp
調査の方向性
まず、既存の Con.1 と ES.71 のガイダンス、およびこの issue に寄せられた 5 件のコメントを確認します。提示されたルールが、引用されている range-based-for のケースを含め、それらの推奨事項を置き換えるべきか、明確化すべきかを判断します。完了とは、決定に達し、合意したガイダンスを C++ Core Guidelines の関連セクションに記録することです。
索引モデルが issue の本文から書いたものです。
説明
A question which comes up every now and then is what type of loop variable you should use for range-based for, particularly when no mutation takes place.
// case (1) - cheap to copy
for (int x : container) // disallowed in this form by Con.1, should be 'const int'
// vs
for (const int &x : container)
// case (2) - expensive to copy
for (string x : container) // disallowed in this form by ES.71, should be 'const string &x'
// vs
for (const string &x : container)
I think we should recommend to always declare the loop variable as a reference, never as a value, and here's why:
- The rule is simple and consistent.
- When mutating a
T&variable in the loop, you are already forced to follow it by the language. - If you aren't, and you have case (2), then ES.71 already tells you to.
- Otherwise, if you are following Con.1, then
const T xis only one character away fromconst T &x, so it costs very little effort to follow this rule. - It is possible and relatively easy to enforce with automatic tooling.
- It prevents bugs related to taking the address of the local variable, instead of taking the address of the object within the container.
- Even for weird iterators like
std::ranges::iota_view::iterator, where theirreferenceis actually a value, this method is robust becauseconst&allows for temporary materialization.
In essence, I don't see any benefit to non-reference loop variables, and the alternative is consistent, easy, correct by default, and already recommended in part.
- 主要言語
- CSS
- スター
- 45.3k
- フォーク
- 5.6k
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
isocpp/CppCoreGuidelines のほかの issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 75/100
isocpp/CppCoreGuidelines#2338 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
isocpp/CppCoreGuidelines#2205 · コメント 8 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 64/100
isocpp/CppCoreGuidelines#1649 · コメント 3 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 42/100
isocpp/CppCoreGuidelines#2336 · コメント 4 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
isocpp/CppCoreGuidelines#2335 · コメント 2 件 ·
isocpp/CppCoreGuidelines の issue をすべて見る
似ている issue
-
add-on doc enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
JuliaGraphics/ColorTypes.jl#344 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
agent-butler-finding chore
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
jordansmall/spindrift#4146 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
DaveGamble/cJSON#1093 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
midnightntwrk/midnight-docs#1430 ·
メンテナーはふだん 1 日以内に返信
-
bug documentation needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
stacklok/docs-website#1185 ·
メンテナーはふだん 1 日以内に返信