-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Open
Open
Copy link
Labels
Description
We often use std::vector instead of std::array in our code so that we don't have to template the methods. With C++20 and std::span, there is an elegant alternative for this. Therefore, it would be useful to have a check that recognizes when the container itself is irrelevant and code can be written more generically. Here is a code example that shows how using std::span eliminates the need for the template:
#include <iostream>
#include <span>
#include <vector>
constexpr int sum(const std::span<const int>& values)
{
int sum = 0;
for(auto value : values)
{
sum += value;
}
return sum;
}
int main()
{
constexpr auto values1 = std::to_array({0, 2, 1, 3});
const std::vector<int> values2{4, 7, 8, 9};
std::cout << sum(values1) << '\n';
std::cout << sum(values2) << '\n';
return 0;
}This check would be interesting in combination with #146607. In other words, with this suggestion, Clang-Tidy would recognize where generic containers can be passed, and with #146607, it would then recognize that, for example, a std::vector can be converted to a std::array because a method expects a std::span.