Skip to main content
11 events
when toggle format what by license comment
Jun 10, 2024 at 20:18 comment added indi Ah, crap, I was only thinking of a non-template string view in my head; I should know better because I run into this all the time. Yes, you need a second function to convert to string views, but that is the better way to go. Notice that it’s only the converting wrapper that gets instantiated 4 extra times, not the actual implementation. And that’s without optimizations; normally the wrapper will be optimized away completely… the actual function will not.
Jun 10, 2024 at 20:17 comment added indi I would suggest a concept that detects implicit conversions to string views (not explicit conversions like string_view_compatible), and then a same-as check, like this. I would strongly discourage implicitly accepting types that only explicitly convert to a string view (like, say std::vector<char>), because that will include random ranges and views. Only things that implicitly convert to string views should be considered “stringy”. (If someone really wants to coerce something random into a string view, they can always do so explicitly.)
Jun 10, 2024 at 20:17 comment added indi What’s being checked is that a and b are explicitly convertible to string views, and you can use the .find() method of a with b. But the only possible way you can use basic_string_view::find() on another string view is if they are both the same type of string view. That is what is really being checked (because there really isn’t a question of whether basic_string_view has a .find() method)… and that is what you want to be checking (because if the two string views are the same type, that naturally implies you can search for one in the other, regardless of search method).
Jun 10, 2024 at 20:17 comment added indi Yes, I can guarantee with 100% certainty that there will be people confused about whether the requires clause calls .find(). Ad hoc requires expressions are a code smell for that and other reasons. In this case, it’s not only making the interface depend on the implementation (what if you want to use a different method, like ranges::search(), a custom binary search, or a Boyer–Moore searcher?), it’s obfuscating what’s really being checked.
Jun 10, 2024 at 7:10 comment added Toby Speight The trouble with the template<typename CharT, typename Traits> approach is that it doesn't match anything except std::basic_string_view<> - no conversions from string literals or std::basic_string<> objects, for example. So we'd need more templates to match them and forward to this one.
Jun 10, 2024 at 7:06 comment added Toby Speight Is it because the find() in the requires clause looks like it's calling find()? I guess that someone unfamiliar with concepts might be misled, in which case maybe it could be extracted out into a named concept, perhaps using std::is_same_v<> as in the string_view_compatible I wrote for this SO answer?
Jun 10, 2024 at 6:57 comment added Toby Speight @indi, why would one think the requires would ensure that b is a substring of a? The function quite clearly has to accept b that is not a substring of a (and return a "not found" value). The constraint just needs to ensure that the code can perform that check.
Jun 10, 2024 at 0:54 comment added indi If you do want to require b is a sub-string of a, that is a job for contracts… not concepts. Concepts check types; contracts check values. So, using the currently-proposed P2900 contracts syntax, you’d want template <typename C, typename T> auto min_length(std::basic_string_view<C, T> a, std::basic_string_view<C, T> b, …) pre (a.contains(b)) …. Of course, we don’t have contracts in the language yet, so you’d have to fake it with a manual check and then throw/assert/whatevs.
Jun 10, 2024 at 0:52 comment added indi That requires clause is more than a little misleading… and ultimately pointless. All you’re really checking is if S1 and S2 are both convertible to the same type of string view; you are not checking whether b is a sub-string of a. It would make more sense to just do template <typename C, typename T> auto min_length(basic_string_view<C, T> a, basic_string_view<C, T> b, …, because there is no sense generating different functions for min_length("", …, min_length(""s, …, min_length(""sv, … (etc. for anything that converts to a string view, and every cv-ref variation).
Jun 9, 2024 at 13:56 comment added Madagascar "int cnt is a poor choice for a count" ==> Absolutely, also sounds like vulgar slang to me. :)
Jun 9, 2024 at 13:34 history answered Toby Speight CC BY-SA 4.0