Guideline for how to declare variables in a range-based for loop
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:
- 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.
- 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
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của isocpp/CppCoreGuidelines
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
isocpp/CppCoreGuidelines#2205 · 8 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 64/100
isocpp/CppCoreGuidelines#1649 · 3 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
isocpp/CppCoreGuidelines#2335 · 2 bình luận ·
-
Provide a simple GSL utility to replace `static_cast<void>`Có thể làm lại được @GabrielDosReis đã nhận 210 ngày trước và không có pull request nào đang mở. Đang mở
isocpp/CppCoreGuidelines#2319 · 7 bình luận · 1 người được giao ·
-
Update guidelines for existence of C++20 modulesCó thể làm lại được @GabrielDosReis đã nhận 259 ngày trước và không có pull request nào đang mở. Đang mở
isocpp/CppCoreGuidelines#2295 · 1 bình luận · 1 người được giao ·
Tất cả issue của isocpp/CppCoreGuidelines
Issue tương tự
-
area:docs good first issue P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
uttrflow/uttrflow-swift#3445 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
adr
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
kristofdegrave/homeassistant-smart-charging#1607 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Feature
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
Narezzurri/OpenVPN-Config-Manager#95 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
FAQ- what is an NHS EmployerĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
doc good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
collective/icalendar#1865 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày