Comparing default-constructed iterators with operator==

前端 未结 4 897
清歌不尽
清歌不尽 2021-01-02 03:21

Does the C++ Standard say I should be able to compare two default-constructed STL iterators for equality? Are default-constructed iterators equality-comparable?

I w

相关标签:
4条回答
  • 2021-01-02 04:00

    This is going to change in C++14. [forward.iterators] 24.2.5p2 of N3936 says

    However, value-initialized iterators may be compared and shall compare equal to other value-initialized iterators of the same type.

    0 讨论(0)
  • 2021-01-02 04:02

    I believe you should pass a range to the function.

    void fun(std::list<int>::iterator beg, std::list<int>::iterator end)
    {
        while(beg != end)
        {
            // do what you want here.
            beg++;
        }
    }
    
    0 讨论(0)
  • 2021-01-02 04:04

    OK, I'll take a stab. The C++ Standard, Section 24.1/5:

    Iterators can also have singular values that are not associated with any container. [Example: After the declaration of an uninitialized pointer x (as with int* x;), x must always be assumed to have a singular value of a pointer. ] Results of most expressions are undefined for singular values; the only excep- tion is an assignment of a non-singular value to an iterator that holds a singular value.

    So, no, they can't be compared.

    0 讨论(0)
  • 2021-01-02 04:08

    Specification says that the postcondition of default constructor is that iterator is singular. The comparison for equality are undefined, so it may be different in some implementation.

    0 讨论(0)
提交回复
热议问题