C++ Range-Based For Error — Complete Guide
이 글의 핵심
C++ range-based for error C++, for loop, "no, Introduction: "Getting error in range-based for" explained in detail with practical examples.
Introduction: “Getting Error in Range-Based For"
"for (auto x : vec) doesn’t work”
C++11’s range-based for loop makes container iteration concise, but incorrect usage causes compile errors or crashes.
// ❌ Error code
for (auto x : myCustomType) { // No begin()/end()
std::cout << x << '\n';
}
// error: no matching function for call to 'begin(MyCustomType&)'
What This Guide Covers:
- 8 range-based for errors
- begin()/end() requirements
- Value vs reference capture
- Iterator invalidation caution
- Temporary object lifetime
1. How Range-Based For Works
Internal Conversion
// Range-based for
for (auto x : vec) {
std::cout << x << '\n';
}
// Compiler converts (conceptually)
{
auto&& __range = vec;
auto __begin = std::begin(__range);
auto __end = std::end(__range);
for (; __begin != __end; ++__begin) {
auto x = *__begin;
std::cout << x << '\n';
}
}
Requirement: begin() and end() functions needed.
2. Eight Common Errors
Error 1: no begin function
// ❌ No begin()/end()
struct MyRange {
int data[5] = {1, 2, 3, 4, 5};
};
MyRange range;
for (auto x : range) { // No begin()/end()
std::cout << x << '\n';
}
// error: no matching function for call to 'begin(MyRange&)'
Solution: Define begin()/end().
// ✅ Member functions
struct MyRange {
int data[5] = {1, 2, 3, 4, 5};
int* begin() { return data; }
int* end() { return data + 5; }
};
// ✅ Or non-member functions
int* begin(MyRange& r) { return r.data; }
int* end(MyRange& r) { return r.data + 5; }
Error 2: Value Copy (Not Modified)
// ❌ Copy (original not modified)
std::vector<int> vec = {1, 2, 3, 4, 5};
for (auto x : vec) { // Value copy
x = 99; // Modify copy
}
// vec is still {1, 2, 3, 4, 5}
// ✅ Modify with reference
for (auto& x : vec) { // Reference
x = 99;
}
// vec is now {99, 99, 99, 99, 99}
Error 3: const Mismatch
// ❌ Non-const reference on const object
void print(const std::vector<int>& vec) {
for (auto& x : vec) { // auto& → int&
std::cout << x << '\n';
}
}
// error: binding reference of type 'int&' to 'const int' discards qualifiers
// ✅ const reference
void print(const std::vector<int>& vec) {
for (const auto& x : vec) { // const auto&
std::cout << x << '\n';
}
}
Error 4: Modification During Iteration (Invalidation)
// ❌ push_back during iteration
std::vector<int> vec = {1, 2, 3, 4, 5};
for (auto x : vec) {
vec.push_back(x * 2); // ❌ Reallocation → iterator invalidation → crash
}
// ✅ Store size beforehand
std::vector<int> vec = {1, 2, 3, 4, 5};
size_t size = vec.size();
for (size_t i = 0; i < size; ++i) {
vec.push_back(vec[i] * 2);
}
Summary
Key Points
- begin()/end(): Required for range-based for
- Value vs reference: auto copies, auto& references
- const correctness: Use const auto& for const containers
- Iterator invalidation: Don’t modify container during iteration
- Temporary objects: Dangerous in C++11/14, safe in C++17
When to Use
✅ Use range-based for when:
- Simple container iteration
- Don’t need index
- Read-only or modify elements
❌ Don’t use when:
- Need to modify container structure
- Need index
- Complex iteration logic
Best Practices
- ✅ Use const auto& for large objects
- ✅ Use auto& to modify elements
- ✅ Store size before modifying container
- ❌ Don’t push_back during iteration
- ❌ Don’t iterate over temporary objects (C++11/14)
Related Articles
Master range-based for for cleaner C++ code! 🚀
Frequently Asked Questions (FAQ)
Q. When would I use this in practice?
A. Everything about C++ Range-Based For Error : from basic concepts to practical applications.
Q. What should I read before this?
A. Follow the previous article or related articles links at the bottom of each post to learn in sequence. See the C++ series index for the full picture.
Q. Where can I study this more deeply?
A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.
Related Articles (Internal Links)
Other articles related to this topic.
- C++ 반복자 기초 완벽 가이드 | iterator 카테고리·begin/end·역방향 반복자·실전 패턴
- C++ 반복자 무효화 에러 | ‘vector iterators incompatible’ 크래시 완벽 해결
- C++ 범위 기반 for문과 구조화된 바인딩 | 모던 C++ 반복문
Keywords Covered in This Article (Related Search Terms)
This article covers C++, range-based-for, error-resolution, iterator, C++11.