Is comparison of const_iterator with iterator well-defined?

前端 未结 3 992
清歌不尽
清歌不尽 2020-12-15 15:50

Consider the following code:

#include 
#include 

int main()
{
    std::vector vec{1,2,3,5};
    for(auto it=vec.cbe         


        
3条回答
  •  独厮守ぢ
    2020-12-15 16:35

    Surprisingly, C++98 and C++11 didn't say that you can compare a iterator with a const_iterator. This leads to LWG issue 179 and LWG issue 2263. Now in C++14, this is explicitly permitted by § 23.2.1[container.requirements.general]p7

    In the expressions

    i == j
    i != j
    i < j
    i <= j
    i >= j
    i > j
    i - j
    

    where i and j denote objects of a container's iterator type, either or both may be replaced by an object of the container's const_iterator type referring to the same element with no change in semantics.

提交回复
热议问题