What is singular and non-singular values in the context of STL iterators?

前端 未结 4 624
北海茫月
北海茫月 2020-12-06 05:54

The section §24.1/5 from the C++ Standard (2003) reads,

Just as a regular pointer to an array guarantees that there is a pointer value pointing pa

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 06:42

    What is singular value and nonsingular value? How are they defined? And where?

    Let us use the simplest incarnation of an Iterator: the pointer.

    For a pointer:

    • the singular value alluded to is the NULL value an uninitialized value.
    • a non-singular value is an explicitly initialized value, it may not be dereferencable still (the past-the-end pointer shall not be dereferenced)

    I would say that the NULL pointer is a singular value, though not the only one, since it represents the absence of value.

    What is the equivalence for regular iterators ?

    std::vector::iterator it;, the default constructor of most iterators (those linked to a container) create a singular value. Since it's not tied to a container, any form of navigation (increment, decrement, ...) is meaningless.

    How and why dereferenceable values are always nonsingular ?

    Singular values, by definition, represent the absence of a real value. They appear in many languages: Python's None, C#'s null, C's NULL, C++'s std::nullptr. The catch is that in C or C++, they may also be simple garbage... (whatever was there in memory before)

    Is a default constructed iterator a singular value ?

    Not necessarily, I guess. It is not required by the standard, and one could imagine the use of a sentinel object.

提交回复
热议问题