isocpp / CppCoreGuidelines

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
Other
42.7k stars 5.44k forks source link

What exactly is `span_p<>`? #1518

Open mbeutel opened 5 years ago

mbeutel commented 5 years ago

The Core Guidelines specify span_p<> like this:

  • span_p<T> // {p, predicate} [p:q) where q is the first element for which predicate(*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?

vmgolovin commented 4 years ago

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.

cubbimew commented 4 years ago

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

BjarneStroustrup commented 4 years ago

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.

shaneasd commented 4 years ago

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.

MikeGitb commented 4 years ago

I don't think span_p actually exists does it?

cubbimew commented 4 years ago

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.