diff --git a/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md b/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md index c3d45bedb..997630e29 100644 --- a/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md +++ b/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md @@ -37,3 +37,74 @@ int main() { std::cout << "After Swap: x = " << x << " y = " << y << '\n'; // Outputs 10 5 } ``` + + +## References in Range-based For Loops + +When iterating over containers such as `std::vector`, the choice of `auto`, `auto &`, `auto const &`, or `const auto` makes a big difference: + +```cpp +std::vector stooges {"xyz", "abc", "def"}; + +// Read-only, no copies +for (auto const &str : stooges) + std::cout << str << std::endl; + +// Makes a copy of each string +for (auto str : stooges) + std::cout << str << std::endl; + +// Direct reference, can modify original elements +for (auto &str : stooges) + str += "!"; + +// Makes a copy of each string, but prevents modification of the copy +for (const auto str : stooges) + std::cout << str << std::endl; +``` + +### Comparison + +| Loop style | Copies made? | Can modify element? | Efficiency | Typical Use Case | +|----------------------|--------------|-----------------------|----------------------------------|-------------------------------------| +| `auto str` | ✅ Yes | ✅ Only the copy | Less efficient for large objects | When you need a local, mutable copy | +| `auto const &str` | ❌ No | ❌ Read-only | Most efficient for read-only use | Safely read elements without copying | +| `auto &str` | ❌ No | ✅ Modifies original | Efficient, mutates container | Modify elements in place | +| `const auto str` | ✅ Yes | ❌ Read-only | Less efficient for large objects | Explicit read-only copy | + +--- + +## Example: Why `const auto str` Can Be Useful + +Sometimes you want to make sure you **don’t accidentally modify** the loop variable, even though it’s just a copy. Declaring it `const` ensures this safety: + +```cpp +std::vector stooges {"xyz", "abc", "def"}; + +for (const auto str : stooges) { + // str += "!"; // ❌ Error: str is const, cannot be modified + std::cout << str << std::endl; // Safe read-only copy +} +``` + +This is useful when: +- You **don’t need references** but still want to guarantee immutability inside the loop body. +- You want to make your **intent explicit**: the loop variable should not be changed. + +--- + +## Rule of Thumb + +- Use **`auto const &`** for **read-only access** (best efficiency). +- Use **`auto &`** when you need to **modify elements in place**. +- Use **`auto`** when you need a **mutable copy**. +- Use **`const auto`** when you want a **read-only copy** inside the loop to prevent accidental modification. + +--- + +## Performance Notes + +- For **large objects** like `std::string`, `std::vector`, or custom classes, prefer **`auto const &`** to avoid unnecessary copies. +- For **small, cheap-to-copy types** like `int`, `char`, or `bool`, using `auto` or `const auto` is fine and often simpler. +- `auto &` should only be used when you *intend* to modify the elements in place, since it directly mutates the container. +