CppCoreGuidelines
CppCoreGuidelines copied to clipboard
What exactly is `span_p<>`?
The Core Guidelines specify span_p<>
like this:
span_p<T>
//{p, predicate}
[p:q)
whereq
is the first element for whichpredicate(*p)
is true
But why would this be a dedicated type rather than simply a span<>
algorithm?
template <typename T, typename PredT>
span<T> take_until(span<T> s, PredT&& pred)
{
...
}
Perhaps I'm misunderstanding the intention of span_p<>
? Could somebody elaborate?
I think the concept arose from views into zero-terminated strings. std::string_view
has no information about is the referenced part of string is zero-terminated, which can be useful.
I'd say C++20 ranges include this functionality as views::take_while. For example,
int a[] = {1,2,3,0,-1,-2,-3};
auto up_to_zero = views::take_while(a, [](int n){return n != 0;});
for (int n: up_to_zero) std::cout << n; // prints 123
live demo: https://wandbox.org/permlink/OX4smWQLC8uOh6PJ
views into zero-terminated strings.
That's zstring
from F.25
On 10/1/2020 9:47 PM, Sergey Zubkov wrote:
I'd say C++20 ranges include this functionality as views::take_while https://en.cppreference.com/w/cpp/ranges/take_while_view. For example,
|int a[] = {1,2,3,0,-1,-2,-3}; auto up_to_zero = views::take_while(a, [](int n){return n != 0;}); for (int n: up_to_zero) std::cout << n; // prints 123|
|Yes. I wouldn't call that pretty, though and I worry about |
|int a[] = {1,2,3,9,-1,-2,-3}; auto up_to_zero = views::take_while(a, [](int n){return n != 0;}); for (int n: up_to_zero) std::cout << n; // prints what? |
||
||
live demo: https://wandbox.org/permlink/OX4smWQLC8uOh6PJ https://wandbox.org/permlink/OX4smWQLC8uOh6PJ
views into zero-terminated strings.
That's |zstring| from F.25
— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/isocpp/CppCoreGuidelines/issues/1518#issuecomment-702359379, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACTNGNCVK6E52NZ7H7SEAFDSITMGFANCNFSM4IYBPQLQ.
is span_p(a, [](int n){ return n!= 0; })
prettier than take_while(a, [](int n){ return n != 0; })
or am I misunderstanding the syntax?
Does http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p13-use-support-libraries-as-appropriate apply here? I would have assumed stl > gsl > libraries > my code. Though technically what P.13 says is stl = gsl > libraries > my code. I always assumed that was because the gsl was assumed to be designed with P.13 in mind.
I don't think span_p actually exists does it?
int a[] = {1,2,3,9,-1,-2,-3}
in that case the take_while is just a view into the original range, in this case, finite range with 7 elements in it
I don't think span_p actually exists does it?
No in any existing GSL, because the Guidelines are not specific enough about it.