Open
Description
Needs a check that suggests using views std::views::keys
, std::views::values
and std::views::elements
in foreach loop.
Assume we have a map and vector of tuples:
std::map<std::string, double> sample_map;
std::vector<std::tuple<std::string, double, bool>> sample_tuples;
BEFORE:
for (const auto& [name,_] : sample_map)
std::cout << name << std::endl;
for (const auto& kv : sample_map) {
const double mass = kv.second;
std::cout << mass << std::endl;
}
for (const auto& elements : sample_tuples)
std::cout << std::get<2>(elements) << std::endl;
AFTER:
for (std::string const& name : std::views::keys(sample_map))
std::cout << name << std::endl;
for (const double mass : std::views::values(sample_map))
std::cout << mass << std::endl;
for (const bool element2 : std::views::elements<2>(sample_tuples))
std::cout << element2 << std::endl;
Keep in mind that we should avoid using auto
for simple typenames like std::string
, double
, bool
, etc.. (does everybody agree with that? any doubts - it should be as an option) due to readability reasons.
Also I don't see any reason to use as const reference any trivial types, like double
, bool
, etc.
Also it would be nice to have an option to enable pipe sintax:
for (std::string const& name : sample_map | std::views::keys)
std::cout << name << std::endl;
for (const double mass : sample_map | std::views::values)
std::cout << mass << std::endl;
for (const bool element2 : sample_tuples | std::views::elements<2>)
std::cout << element2 << std::endl;