Fixed formatting for references comparison table (#9540)

The formatting is unclear and messy initially, modified as a table instead for clarity.
This commit is contained in:
Kyle Nguyen
2026-01-14 07:28:17 -07:00
committed by GitHub
parent d27425175c
commit 38670635ce

View File

@@ -69,57 +69,13 @@ When iterating over containers such as `std::vector<std::string>`, the choice of
### Comparison
Loop style
| 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 |
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
-------------------------------------------
@@ -156,4 +112,4 @@ 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.
* `auto &` should only be used when you _intend_ to modify the elements in place, since it directly mutates the container.