operator==(std::common_iterator) - cppreference.com

From cppreference.com

template<classI2,std::sentinel_for<I>S2>requiresstd::sentinel_for<S,I2>friendconstexprbooloperator==(constcommon_iterator&x,conststd::common_iterator<I2,S2>&y); (1) (since C++20)template<classI2,std::sentinel_for<I>S2>requiresstd::sentinel_for<S,I2>&&std::equality_comparable_with<I,I2>friendconstexprbooloperator==(constcommon_iterator&x,conststd::common_iterator<I2,S2>&y); (2) (since C++20)Compares the iterators and/or sentinels held by underlying

std::variant

member objects var . Two incomparable iterators or two sentinels are considered equal.

Let Ix be x.var.index() and Iy be y.var.index():

1) If Ix==Iy (i.e. both x and y hold iterators or both hold sentinels), returns true, otherwise returns std::get<Ix>(x.var)==std::get<Iy>(y.var).

2) If Ix==1&&Iy==1 (i.e. both x and y hold sentinels), returns true, otherwise returns std::get<Ix>(x.var)==std::get<Iy>(y.var).

If x.var.valueless_by_exception()||y.var.valueless_by_exception() is true, the behavior is undefined.

(until C++26)If x.var.valueless_by_exception()||y.var.valueless_by_exception() is true:

If the implementation is

hardened

, a

contract violation

occurs.

If the implementation is not hardened, the behavior is undefined.

(since C++26)The != operator is

synthesized

from operator==.

These function templates are not visible to ordinary

unqualified

or

qualified lookup

, and can only be found by

argument-dependent lookup

when std::common_iterator<I> is an associated class of the arguments.

Parameters

x, y - iterator adaptors to compare Return value

true if underlying iterators and/or sentinels are equal, false otherwise.

Example

Run this code

#include<cassert>#include<iterator>intmain(){inta[]{0,1,2,3};usingCI=std::common_iterator<std::counted_iterator<int*>,std::default_sentinel_t>;CIi1{std::counted_iterator{a+0,2}};CIi2{std::counted_iterator{a+1,2}};CIi3{std::counted_iterator{a+0,3}};CIi4{std::counted_iterator{a+0,0}};CIs1{std::default_sentinel};CIs2{std::default_sentinel};assert((i1==i2)==true);assert((i1==i3)==false);assert((i2==i3)==false);assert((s1==s2)==true);assert((i1==s1)==false);assert((i4==s1)==true);}Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 3574

C++20 variant was fully constexpr (P2231R1) but common_iterator was not also made constexpr See also

(C++20)

computes the distance between two iterator adaptors
(function template)

[edit]