Question: This is the requirement for the code:

> 
> Create a generic function `all_equal(start, stop)` that determines
> if the elements in the range `[start,stop)` are all equal. The function returns true or false. If the range is empty, true is returned. The arguments `start` and `stop` are bidirectional iterators.
> 
> 
> 

The following is my code:

```
template <class Iterator>
Iterator all_equal(Iterator start, Iterator stop) {
    Iterator front = start;
    Iterator back = stop;
    while (front != back){
        if (*front == *back)
            return false;
        else {
            front++;
            back--;
        }
    }
}

```

The following is my feedback from a marker:

> 
> You can't dereference back. And this would compare elements two-by-two without ensuring they're all equal.
> 
> 
>

Answer: 
> 
> You can't dereference back.
> 
> 
> 

In C++ we use a 1 past the end notation for our containers. This is reflected in your question.

```
[start, stop)

```

This is mathematical notation. The square bracket `[` means inclusive but the round bracket `')` means not inclusive. So `start` is inside the range and `stop` is outside the range.

In iterate over containers in the same way. The `stop` is one past the end of the range. You can not de-reference this value. The first time around the loop you do de-reference it here:

```
        if (*front == *back)

```

---

> 
> And this would compare elements two-by-two without ensuring they're all equal.
> 
> 
> 

Your code would find the following list all equal (assuming you fixed the back issue above) and the return type.

```
 [ 1, 2, 3, 4, 4, 3, 2, 1]

```

You check both ends and move towards the middle.

---

> 
> The function returns true or false.
> 
> 
> 

No return at the end of the function.

```
            front++;
            back--;
        }
    }
}

```

So we never know if it works!

---

I like Remy's solution, though I prefer a precheck for empty and a `for` rather than `while`.

```
template <class Iterator>
bool all_equal(Iterator start, Iterator stop) {
    if (start == stop) {
        return true;
    }
    for (Iterator loop = start; loop != stop; ++loop) {
        if (*loop != *start) {
            return false;
        }
    }
    return true;
}

```
First of all your function should return `bool` which is `true` or `false`, not iterator. Second - you cannot dereference `stop` as it does not point to an element in the sequence. Third - you either increase `start` or decrease `stop` but not both at the same time. Function could be:

```
template <class Iterator>
bool all_equal(Iterator start, Iterator stop) {
    if( start == stop ) 
        return true;
    while( true ){
        auto tmp = start++;
        if( start == stop ) return true;
        if( *tmp != *start ) return false;
    }
}

```