isocpp / isocpp/CppCoreGuidelines
Ranges require exception to F.19
Nobody has claimed this yet.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
I noticed F.19: For “forward” parameters, pass by TP&& and only std::forward the parameter when my code triggered cppcoreguidelines-missing-std-forward in clang-tidy.
As Nicolai Josuttis and Arthur O'Dwyer mentioned, “If you see code using (deduced) T&& without std::forward<T>, it’s either buggy or it’s C++20 Ranges.” Yes, ranges code often requires a forwarding reference without needing to forward it. The range object may be modified during the iteration, so we cannot capture it with const T&. It can be an rvalue, so we cannot use T& either. Only T&& is viable. However, forwarding it often does no good at all, as shown by the example below:
#include <iostream>
#include <ranges>
#include <vector>
template <typename Rng>
void Access(Rng&& rng)
{
for (const auto& item : rng) {
std::cout << item << '\n';
}
}
int main()
{
std::vector vec{1, 2, 3, 4, 5, 6};
Access(vec | std::views::filter([](int n) { return n % 2 == 0; }));
}
Forwarding rng (anyone wants to write for (const auto& item : std::forward<Rng>(rng))?) is weird, useless, and unnecessary. Doing so would probably cause more confusion: Why forwarding it here? What good does it do?
So I believe an exception should be added to F.19.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing guideline F.19 and the issue's C++20 Ranges example, then examine the surrounding guideline text for how exceptions are expressed. Done means reaching an agreed wording that clearly covers forwarding references used by ranges without unnecessary std::forward, while preserving the intent of F.19.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100