Binary search for random access iterators in C++ I wrote a generic binary search routine in C++ for ranges that are specified by random access iterators. In case the user inputs a range with non-random access iterators, an exception will be thrown. My code is below:

binary_search.h

#ifndef CODERODDE_BINARY_SEARCH_H
#define CODERODDE_BINARY_SEARCH_H

#include &lt;iterator&gt;

namespace coderodde {

    template&lt;typename RandomIt, typename T&gt;
    RandomIt search(RandomIt begin,
                    RandomIt end,
                    T value,
                    std::random_access_iterator_tag)
    {
        RandomIt save_end = end;

        while (begin != end)
        {
            RandomIt middle = begin + ((end - begin) &gt;&gt; 1);

            if (value &lt; *middle)
            {
                end = middle;
            }
            else if (value &gt; *middle)
            {
                begin = middle + 1;
            }
            else
            {
                return middle;
            }
        }

        return save_end;
    }

    template&lt;typename NonRandomIt, typename T&gt;
    NonRandomIt search(NonRandomIt begin,
                       NonRandomIt end,
                       T value,
                       std::input_iterator_tag)
    {
        throw std::runtime_error(
                   "coderodde::search requires random access iterators.");
    }

    template&lt;typename It, typename T&gt;
    It search(It begin, It end, T value)
    {
        return search(begin, end, value, typename std::iterator_traits&lt;It&gt;::iterator_category());
    }
}

#endif /* CODERODDE_BINARY_SEARCH_H */


And the demonstration code is:

#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;list&gt;
#include &lt;stdexcept&gt;
#include &lt;vector&gt;
#include "binary_search.h"

using coderodde::search;
using std::boolalpha;
using std::cerr;
using std::cout;
using std::end;
using std::endl;
using std::list;
using std::runtime_error;
using std::vector;

int main() {
    cout &lt;&lt; "int array:" &lt;&lt; endl;
    int arr[] = { 1, 3, 4, 4, 5, 7, 10, 15, 23 };
    size_t length = sizeof(arr) / sizeof(arr[0]);
    int* p = search(arr, arr + length, 1);
    cout &lt;&lt; "The 1st iterator points to: " &lt;&lt; *p &lt;&lt; endl;
    p = search(arr, arr + length, 7);
    cout &lt;&lt; "The 2nd iterator points to: " &lt;&lt; *p &lt;&lt; endl;
    p = search(arr, arr + length, 23);
    cout &lt;&lt; "The 3rd iterator points to: " &lt;&lt; *p &lt;&lt; endl;

    p = search(arr, arr + length, 8);

    cout &lt;&lt; "8 not found: " &lt;&lt; boolalpha &lt;&lt; (p == end(arr)) &lt;&lt; endl;

    cout &lt;&lt; "int vector:" &lt;&lt; endl;
    vector&lt;int&gt; vec = { 1, 3, 4, 4, 5, 7, 10, 15, 23 };
    vector&lt;int&gt;::iterator it = search(vec.begin(), vec.end(), 1);
    cout &lt;&lt; "The 1st iterator points to: " &lt;&lt; *it &lt;&lt; endl;
    it = search(vec.begin(), vec.end(), 7);
    cout &lt;&lt; "The 2nd iterator points to: " &lt;&lt; *it &lt;&lt; endl;
    it = search(vec.begin(), vec.end(), 23);
    cout &lt;&lt; "The 3rd iterator points to: " &lt;&lt; *it &lt;&lt; endl;

    p = search(arr, arr + length, 8);

    cout &lt;&lt; "8 not found: " &lt;&lt; boolalpha &lt;&lt; (p == end(arr)) &lt;&lt; endl;

    try {
        list&lt;int&gt; lst = { 1, 3, 4, 4, 5, 7, 10, 15, 23 };
        search(lst.begin(), lst.end(), 4);
    } catch (runtime_error&amp; err) {
        cerr &lt;&lt; err.what() &lt;&lt; endl;
    }
}


The output is:



int array:
The 1st iterator points to: 1
The 2nd iterator points to: 7
The 3rd iterator points to: 23
8 not found: true
int vector:
The 1st iterator points to: 1
The 2nd iterator points to: 7
The 3rd iterator points to: 23
8 not found: true
coderodde::search requires random access iterators.



As always, any critique is much appreciated.
I wrote a generic binary search routine in C++ for ranges that are specified by random access iterators.

Very good idea.

In case the user inputs a range with non-random access iterators, an exception will be thrown. My code is below:

Bad idea. You are doing a runtime check for something that can be detected by the compiler. This should force a compile time error.
Code Review
template&lt;typename RandomIt, typename T&gt;
RandomIt search(RandomIt begin,
                RandomIt end,
                T value,
                std::random_access_iterator_tag)

No need for the type T here. In fact putting it here is probably a bad idea. As it allows you to search a vector of double with an integer. Now there is not much you can do with compiler standard conversions but at least you can make sure that the value passed to the function has already been converted to the correct type for comparison.
template&lt;typename RandomIt, typename Cat = typename std::iterator_traits&lt;RandomIt&gt;::iterator_category&gt;
RandomIt search(RandomIt begin,
                RandomIt end,
                typename std::iterator_traits&lt;RandomIt&gt;::value_type const&amp; value,
                std::random_access_iterator_tag)

Also I would not bother passing the fourth parameter that should be a type to the template. Then you can have a template specialization that implements it for random iterators only.
// Generic Declaration with no implementation.
template&lt;typename RandomIt, typename Cat = typename std::iterator_traits&lt;RandomIt&gt;::iterator_category&gt;
RandomIt search(RandomIt begin,
                RandomIt end,
                typename std::iterator_traits&lt;RandomIt&gt;::value_type const&amp; value);

// Partial specialization for Random Iterator
template&lt;typename RandomIt&gt;
RandomIt search&lt;RandomIt, std::random_access_iterator_tag&gt;(
                RandomIt begin,
                RandomIt end,
                typename std::iterator_traits&lt;RandomIt&gt;::value_type const&amp; value)
{
    // Your implementation here.
}

Now the code will generate a compile time error when you use anything but a random access iterator. With a tiny bit of effort you can do this in a single declaration using the new std::enable_if&lt;&gt; but I leave that as an exercise.
Make Intenet clear
        RandomIt middle = begin + ((end - begin) &gt;&gt; 1);

Not everybody will see &gt;&gt; 1 as a divide by 2. Don't do that it just makes the code unclear. Use the divide operator it express intent much better (and does not have any worse performance).
        RandomIt middle = begin + ((end - begin) / 2);