@@ -22,10 +22,8 @@ int val[] = {1, 2, 3};
2222
2323bool flag = false ;
2424
25- // Forward declaration
2625template <std::size_t N> struct Iter ;
2726
28- // Iterator class
2927template <std::size_t N>
3028struct Iter {
3129 using value_type = int ;
@@ -38,43 +36,35 @@ struct Iter {
3836private:
3937 int * ptr_ = nullptr ;
4038
41- // Allow cross-template access to ptr_
4239 template <std::size_t M> friend struct Iter ;
4340
4441public:
45- // Constructors
4642 Iter () = default ;
4743 Iter (int * ptr) : ptr_(ptr) {}
4844 Iter (const Iter&) = default ;
4945 Iter (Iter&& other) noexcept : ptr_(other.ptr_) {}
5046
51- // Cross-template constructor
5247 template <std::size_t M>
5348 Iter (const Iter<M>& other) : ptr_(other.ptr_) {}
5449
55- // Assignment operators
5650 Iter& operator =(const Iter&) = default ;
5751 Iter& operator =(Iter&& other) noexcept {
5852 ptr_ = other.ptr_ ;
5953 return *this ;
6054 }
6155
62- // Dereference and access
6356 reference operator *() const { return *ptr_; }
6457 pointer operator ->() const { return ptr_; }
6558 reference operator [](difference_type n) const { return ptr_[n]; }
6659
67- // Increment and decrement
6860 Iter& operator ++() { ++ptr_; return *this ; }
6961 Iter operator ++(int ) { auto tmp = *this ; ++*this ; return tmp; }
7062 Iter& operator --() { --ptr_; return *this ; }
7163 Iter operator --(int ) { auto tmp = *this ; --*this ; return tmp; }
7264
73- // Arithmetic operations
7465 Iter& operator +=(difference_type n) { ptr_ += n; return *this ; }
7566 Iter& operator -=(difference_type n) { ptr_ -= n; return *this ; }
7667
77- // Declare friend operators (no inline definition)
7868 template <std::size_t X>
7969 friend Iter<X> operator +(Iter<X> it, difference_type n);
8070
@@ -87,15 +77,13 @@ struct Iter {
8777 template <std::size_t X, std::size_t Y>
8878 friend difference_type operator -(Iter<X> a, Iter<Y> b);
8979
90- // Comparison operators
9180 friend bool operator ==(Iter a, Iter b) = default ;
9281 friend bool operator <(Iter a, Iter b) { return a.ptr_ < b.ptr_ ; }
9382 friend bool operator >(Iter a, Iter b) { return a.ptr_ > b.ptr_ ; }
9483 friend bool operator <=(Iter a, Iter b) { return a.ptr_ <= b.ptr_ ; }
9584 friend bool operator >=(Iter a, Iter b) { return a.ptr_ >= b.ptr_ ; }
9685};
9786
98- // Define operators outside the class
9987template <std::size_t X>
10088inline Iter<X> operator +(Iter<X> it, std::ptrdiff_t n) {
10189 return Iter<X>(it.ptr_ + n);
0 commit comments