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 need to return a `bool` not an `Iterator`. And you are dereferencing the `stop` iterator on the 1st loop iteration, which will be *undefined behavior* if `stop` is the `end` iterator of a container. And there is no need to dercrement the `stop` iterator at all, even though the requirement specifies it as a bidirectional iterator.

Try something more like this instead:

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

```
> 
> 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;
}

```