C + + Pit Point Collection-2 strict Multipass Guarantee

Source: Internet
Author: User
Tags stream api

Before writing a char32_iterator, it simply encapsulates a string::const_iterator that, when operator*, returns the utf-8 sequence it references to a single character utf-32 encoded-this looks simple. At ordinary times, you will encounter similar requirements in the process of programming: Implement an inert transform, apply a conversion function on each element of a container, but not apply it immediately, but wait for the instant conversion when it is used. This sounds like C # LINQ or Java's Stream API. If you are familiar with C + +, you will think of encapsulating an iterator, just like the char32_iterator in front of me, making a transformation of the original sequence of references-the transformation is mostly a pure function that returns the transformed value when operator* is called.

There is nothing wrong with thinking, and when you write, you will encounter a problem, that is, the encapsulated layer of the iterator-we later call him proxy iterator--should belong to what kind of iterator category? Is the category that inherits the encapsulated iterator? If the transform function is a pure function, does it mean that the iterator can scan the entire sequence repeatedly and return the result unchanged, satisfying the Multipass guarantee so that at least it can be a forward iterator?

The result is brutal, the proxy iterator, regardless of the category of the iterator it encapsulates, it can only be an input iterator, which is the weakest iterator. Why is it? Because it does not meet Multipass Guarantee.

The definition of Multipass guarantee here

Simply put, Multipass guarantee not only requires the iterator to allow the copy and is still valid at the original location after the dereference, but also requires the same two iterators, the resulting object should be the same object, and further, the same object refers to the same address. This requires that our iterators at least point to a sequence that determines existence in order to have the necessary conditions for satisfying multipass guarantee. As the above-mentioned proxy iterator, at the time of the dereference, return the result of the transformation immediately, is a right value, even if you are in advance to save in the iterator, return its reference, also does not satisfy the same iterator points to the same object requirements, so this does not satisfy the Multipass Guarantee.

Finally, the category of our proxy iterator has to fall back to the input iterator, the last problem is that the iterator's operator* requires a return reference, and our iterator returns an rvalue when it is dereferenced, is that OK? Yes, cppreference again, not forward Iterator input iterator,operator* The return value can be not a reference-can be the right value, or a proxy class or whatever you like-this hole is good, Otherwise I do not know how to achieve cpplinq.

P.S. This article is very heavy in translation, I do not know why, but the article is really my original.

C + + Pit Point Collection-2 strict Multipass Guarantee

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.