Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Guideline for how to declare variables in a range-based for loop

Đang mở
#2,115 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp
Lĩnh vực
documentation

Hướng nghiên cứu

Bắt đầu bằng việc xem xét hướng dẫn hiện có về Con.1 và ES.71 cùng năm bình luận về issue này. Xác định liệu quy tắc được đề xuất nên thay thế hay làm rõ các khuyến nghị đó, bao gồm cả các trường hợp range-based-for được trích dẫn. Công việc được xem là hoàn tất khi đã đi đến quyết định và ghi lại hướng dẫn đã thống nhất trong phần liên quan của C++ Core Guidelines.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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:

  1. The rule is simple and consistent.
  2. When mutating a T& variable in the loop, you are already forced to follow it by the language.
  3. If you aren't, and you have case (2), then ES.71 already tells you to.
  4. Otherwise, if you are following Con.1, then const T x is only one character away from const T &x, so it costs very little effort to follow this rule.
  5. It is possible and relatively easy to enforce with automatic tooling.
  6. It prevents bugs related to taking the address of the local variable, instead of taking the address of the object within the container.
  7. Even for weird iterators like std::ranges::iota_view::iterator, where their reference is actually a value, this method is robust because const& 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.

Ngôn ngữ chính
CSS
Star
45.3k
Fork
5.6k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của isocpp/CppCoreGuidelines

Tất cả issue của isocpp/CppCoreGuidelines

Issue tương tự

Thêm issue về Documentation

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.