Is there an already defined concept for specifying a range of a specific type, in C++20 or in the ranges-TS?
Something like:
template < class T, class InnerType >
concept RangeOf =
requires(T&& t) {
requires std::same_as<
std::remove_cvref_t<decltype(*std::ranges::begin(t))>,
InnerType
>;
std::ranges::end(t);
};
To allow, for example:
void print(const RangeOf<char> auto& char_seq) { /* ... */ }
No there isn't.
The way to spell that particular concept would be:
But then it turns out there's a bunch of very closely related things you might want to check as well. You might actually want to look at the
referencetype rather than thevalue_type. Perhaps that's:The reason is: do you consider
vector<char>&to be a range ofcharor a range ofchar&? Arguably, it's more like the latter, and it's the latter that drives a lot more usage. But it's not that the former is wrong; it's useful too, just in a different way.Then you have to talk about whether you want
same_asorconvertible_to. There are some algorithms in which you would want to restrict to the former, and some in which the latter is good enough.This issue of
same_asvsconvertible_to, to me, is one of the motivating reasons for wanting concept template parameters, so that:So that I can easily write
RangeOf<same_as<char>>orRangeOf<convertible_to<char>>, depending on what I actually want. However, this brings up other parsing issues too (ifRangeOftakes a unary concept, how do you makesame_as<char>- which is not a unary concept but rather a partially applied concept that is not valid in all contexts - work?) so it's unclear how to really make this work. There's currently a proposal for this, but it would not actually handle this use-case especially well (largely because it's not actually clear how to handle this use-case especially well).All of which is to say, it's hard to know what actual thing we would want for
RangeOf, which is why no such thing exists. But each specific thing that would be useful is very easy to write, so the lack of such a thing isn't a especially large burden on the user.