STL-like palindrome checking I just read this article on how to write a standard-like algorithm and wanted to try it myself by writing (as suggested in the article) an algorithm to test if a sequence given by two bidirectional iterators is a palindrome.

Here is my code with two different ways of calling the algorithm (is_palindrome and alt_is_palindrome):

#include &lt;cctype&gt;       // std::tolower
#include &lt;functional&gt;   // std::equal_to
#include &lt;iostream&gt;     // std::cout
#include &lt;iterator&gt;     // std::iterator_traits&lt;T&gt;::value_type
#include &lt;string&gt;
#include &lt;vector&gt;

template&lt;typename Iterator&gt;
using ItVType  = typename std::iterator_traits&lt;Iterator&gt;::value_type;

template&lt;typename Iterator&gt;
using StdEqual= typename std::equal_to&lt;ItVType&lt;Iterator&gt;&gt;;

template&lt;typename BidirectionalIterator, typename Compare=StdEqual&lt;BidirectionalIterator&gt;&gt;
bool is_palindrome(BidirectionalIterator first, BidirectionalIterator last, Compare comp=Compare())
{
    for(; first != last &amp;&amp; first != --last; ++first)
        if(not comp(*first,*last))
            return false;

    return true;
}

template&lt;typename BidirectionalIterator, typename Compare&gt;
bool alt_is_palindrome(BidirectionalIterator first, BidirectionalIterator last, Compare comp)
{
    for(; first != last &amp;&amp; first != --last; ++first)
        if(not comp(*first,*last))
            return false;

    return true;
}

template&lt;typename BidirectionalIterator&gt;
inline bool alt_is_palindrome(BidirectionalIterator first, BidirectionalIterator last)
{
    return alt_is_palindrome(first, last, StdEqual&lt;BidirectionalIterator&gt;());
}

int main()
{
    std::vector&lt;std::string&gt; vs = { "", "palindrome", "abba", "abcBA"};

    std::cout &lt;&lt; "case sensitive:" &lt;&lt; std::endl;
    for(auto const&amp; s : vs)
        std::cout &lt;&lt; s &lt;&lt; " is "
            &lt;&lt; ( is_palindrome(begin(s),end(s)) ? "" : "not " )
            &lt;&lt; "a palindrome." &lt;&lt; std::endl;
    std::cout &lt;&lt; std::endl;


    auto my_comp = [](auto const&amp; lhs, auto const&amp; rhs)
    {
        return tolower(lhs) == std::tolower(rhs);
    };

    std::cout &lt;&lt; "case insensitive:" &lt;&lt; std::endl;
    for(auto const&amp; s : vs)
        std::cout &lt;&lt; s &lt;&lt; " is "
            &lt;&lt; ( is_palindrome(begin(s),end(s), my_comp) ? "" : "not " )
            &lt;&lt; "a palindrome." &lt;&lt; std::endl;
}


So first: I like the style of is_palindrome better with the default arguments, but I haven't seen it used in that way before (maybe I just haven't seen enough code...).
Is there a preferred way to declare this?
If so, is it 'just' a convention or are there actual benefits (performance, security,...) to it?

The second thing I am not sure about are my range-based for-loops and the lambda function int the main function.
As you see I took their respective variables by auto const&amp;. I have sometimes seen people take them as auto&amp;&amp;, but I am not quite sure about he implications of this. Is this way preferred?

If you note anything else that isn't quite right or nice (e.g. the kind of type aliases I use), please let me know, too.
Nothing wrong with these:

template&lt;typename Iterator&gt;
using ItVType  = typename std::iterator_traits&lt;Iterator&gt;::value_type;

template&lt;typename Iterator&gt;
using StdEqual= typename std::equal_to&lt;ItVType&lt;Iterator&gt;&gt;;


But I find them a bit disturbing being so unbound (they are in the global namespace). Maybe just a feeling. I personally would put them in an anonymous namespace.

I also prefer

template&lt;typename BidirectionalIterator,
         typename Compare=StdEqual&lt;BidirectionalIterator&gt;&gt;
bool is_palindrome(BidirectionalIterator first,
                   BidirectionalIterator last, 
                   Compare               comp=Compare())


Over

template&lt;typename BidirectionalIterator,
         typename Compare&gt;
bool alt_is_palindrome(BidirectionalIterator first,
                       BidirectionalIterator last,
                       Compare               comp)

template&lt;typename BidirectionalIterator&gt;
inline bool alt_is_palindrome(BidirectionalIterator first,
                              BidirectionalIterator last)


Putting it all in one function does not reduce readability.
But the huge template type names do bother me slightly. Template names are usually short (because they are not real type).

template&lt;typename I,typename Compare=StdEqual&lt;I&gt;&gt;
  // requires BindirectionalIterator&lt;I&gt;()
bool is_palindrome(I first, I last,Compare comp=Compare())


I change my mind this is fine.

for(; first != last &amp;&amp; first != --last; ++first)
    if(not comp(*first,*last))
        return false;


But you may want to look at some algorithms.

return std::equal(first, last, std::reverse_iterator(last), comp);


Admittedly I now compare twice as many elements (so not an improvement in this case (informational only)). But if you are using random access elements you can reduce this by calculating the mid point manually.

auto dist    = std::distance(first, last);
auto halfway = first;
std::advance(halfway, dist/2);   
return std::equal(first, halfway, std::reverse_iterator(last), comp);


Looks like this should not compile:

 is_palindrome(begin(s),end(s))


That should be std::begin() and std::end() so it looks like you have a hidden using namespace std; somewhere.

If you are going to declare your lambda like this:

auto my_comp = [](auto const&amp; lhs, auto const&amp; rhs)
{
    return tolower(lhs) == std::tolower(rhs);
};


You may as well put it in a separate function. The point of lambdas is you can see them in place.

is_palindrome(begin(s),end(s),
              [](auto const&amp; lhs, auto const&amp; rhs) {return tolower(lhs) == std::tolower(rhs);}
             );