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

前端 未结 4 615
北海茫月
北海茫月 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:39

    If I understand this correctly, a singular value for an iterator is essentially the equivalent of an unassigned pointer. It's an iterator that hasn't been initialized to point anywhere and thus has no well-defined element it's iterating over. Declaring a new iterator that isn't set up to point to an element of a range, for example, creates that iterator as a singular iterator.

    As the portion of the spec alludes to, singular iterators are unsafe and none of the standard iterator operations, such as increment, assignment, etc. can be used on them. All you can do is assign them a new value, hopefully pointing them at valid data.

    I think the reason for having this definition is so that statements like

    set::iterator itr;
    

    Can be permitted by the spec while having standardized meaning. The term "singular" here probably refers to the mathematical definition of a singularity, which is also called a "discontinuity" in less formal settings.

提交回复
热议问题