Determining if a sequence is a palindrome To complement this Java question on palindrome identification, I came up with this C++(14) version:

#include &lt;iterator&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;

namespace detail
{
    template &lt;typename RandomIt, typename BinaryPredicate&gt;
    bool is_palindrome(RandomIt first, RandomIt last, BinaryPredicate pred,
                       std::random_access_iterator_tag)
    {
        return std::equal(first, std::next(first, std::distance(first, last) / 2),
                          std::make_reverse_iterator(last), pred);
    }

    template &lt;typename BidirIt, typename BinaryPredicate&gt;
    bool is_palindrome(BidirIt first, BidirIt last, BinaryPredicate pred,
                       std::bidirectional_iterator_tag)
    {
        if (first == last || first == --last) return true;

        for (; first != last; ++first, --last) {
            if (!pred(*first, *last)) return false;
            if (std::next(first) == last) break;
        }

        return true;
    }
} // namespace detail

template &lt;typename BidirIt, typename BinaryPredicate&gt;
bool is_palindrome(BidirIt first, BidirIt last, BinaryPredicate pred)
{
    return detail::is_palindrome(first, last, pred,
                                 typename std::iterator_traits&lt;BidirIt&gt;::iterator_category {});
}

template &lt;typename BidirIt&gt;
bool is_palindrome(BidirIt first, BidirIt last)
{
    using V = typename std::iterator_traits&lt;BidirIt&gt;::value_type;
    return detail::is_palindrome(first, last,
                                 std::equal_to&lt;V&gt; {},
                                 typename std::iterator_traits&lt;BidirIt&gt;::iterator_category {});
}

template &lt;typename SequenceType, typename BinaryPredicate&gt;
bool is_palindrome(const SequenceType&amp; sequence, BinaryPredicate pred)
{
    return is_palindrome(std::cbegin(sequence), std::cend(sequence), pred);
}

template &lt;typename SequenceType&gt;
bool is_palindrome(const SequenceType&amp; sequence)
{
    return is_palindrome(std::cbegin(sequence), std::cend(sequence));
}


Used as such:

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;list&gt;

int main()
{
    std::cout &lt;&lt; std::boolalpha;
    std::string str {"abba"};
    std::cout &lt;&lt; is_palindrome(str) &lt;&lt; std::endl;
    std::list&lt;char&gt; lst {str.cbegin(), str.cend()};
    std::cout &lt;&lt; is_palindrome(lst) &lt;&lt; std::endl;
    return 0;
}


Performance is my primary concern here. Comments and suggested improvements are welcome!
Following is my solution. It's much less code. It also handles the case of string literals correctly (e.g., is_palindrome("aba")).

In modern C++ (11 and onward), there is no need to differentiate predicate and non-predicate versions of generic algorithms. The fact that the standard library does overload predicate and non-predicate versions is simply due to historical reasons. See this S.O. topic for details.

#include &lt;functional&gt;
#include &lt;iterator&gt;
#include &lt;utility&gt;

template &lt;typename Iterator, typename Pred = std::equal_to&lt;void&gt;&gt;
bool is_palindrome(Iterator beg, Iterator end, Pred pred = Pred{}) {
  if (beg == end) return true;
  end = std::prev(end);
  if (beg == end) return true;
  do {
    if (! pred(*beg++, *end--)) return false;
  }
  while (beg != end &amp;&amp; beg != std::next(end));
  return true;
}

template &lt;typename T, typename Pred = std::equal_to&lt;void&gt;&gt;
bool is_palindrome(const T&amp; x, Pred pred = Pred{}) {
  return is_palindrome(std::begin(x), std::end(x), std::move(pred));
}

template &lt;std::size_t n, typename Pred = std::equal_to&lt;void&gt;&gt;
bool is_palindrome(const char (&amp;x)[n], Pred pred = Pred{}) {
  return is_palindrome(x, x + n - 1, std::move(pred));
}