The use of decltype or...?

I read at the weekend about STL and came up with a few thoughts.

Let's say you write a class for sorting the same with a single function. Suppose also that you want to do something similar with STL. Look at the description of sort in the STL.

template <class RandomAccessIterator>
 void sort (RandomAccessIterator first, RandomAccessIterator last);

template <class RandomAccessIterator, class Compare>
 void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

1. As usual, by default, objects are compared with operator <.

2. But also, you want a function not just sorted by default, but also took a functional object for comparison (whether pointer to function, functor or lambda expression).

But I want to make myself a sort function was one. Ie, not different implementations with the default sort and a custom comparator with code duplication. One single function for sorting.

Comes to mind to do so. In fact, the sorting code is only in the operation with the comparator. And that, without comparator calls the function with the comparator with the standard functor less.

But take a look at the description less:
template <class T> struct less {
 bool operator() (const T& x, const T& y) const {return x<y;}
 typedef T first_argument_type;
 typedef T second_argument_type;
 typedef bool result_type;

This is a structure to initialize the object which you want to know the type.

Ie you need to write:
std::less<type> comp;

And call:
sort (first, last, comp);

But at this point we do not know.

You can use the new C++11 standard and call:
sort( first, last, std::less< decltype(*first) >() );

But how much is correct (moreover, it is unclear what is dereferenced the pointer, and how to be with older compilers)? How to write better (smaller, prettier) differently? Just for the sake of interest.

It is a working bubble sort, as described above, in order to have a better view
#include <algorithm> // std::swap

class BubbleSort

 template< class RandomAccessIterator >
 static void sort( RandomAccessIterator first, RandomAccessIterator last )
 sort( first, last, std::less< decltype(*first) >() );

 template< class RandomAccessIterator, class Compare >
 static void sort( RandomAccessIterator first, RandomAccessIterator last, Compare comp )
 for( auto i = first; i != last; i++ )
 for( auto j = i + 1; j != last; j++ )
 if( comp( *j, *i ) )
 std::swap( *i, *j );


October 3rd 19 at 02:48
1 answer
October 3rd 19 at 02:50
template< class RandomAccessIterator >
void sort( RandomAccessIterator first, RandomAccessIterator last )
 sort( first, last, std::less< std::iterator_traits<randomaccessiterator>::value_type >() );
Forgot to put typename before std::iterator_traits<randomaccessiterator>::value_type</randomaccessiterator> - Frederick23 commented on October 3rd 19 at 02:53
Here! Thank you. Not for nothing did I read about STL, but didn't finish :) It's all a bit forgettable. - asia10 commented on October 3rd 19 at 02:56

Find more questions by tags C++