N
N
nicolausYes2013-06-25 17:41:37
C++ / C#
nicolausYes, 2013-06-25 17:41:37

Using decltype or...?

I was reading about the STL over the weekend and a few thoughts came to mind.
Suppose you are writing a class for sorting with a single function of the same name. Let's also say that you want to do something similar to the STL. We look at the description of sort in 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 the < operator .
2. But besides, you want the function to not just sort by default, but also accept a functional object for comparison (be it a function pointer, a functor, or a lambda expression).
But there is a wish to make so that function of sorting was one. Those. not different implementations with default sorting and custom comparator with code duplication. One single function for sorting.
It comes to mind to do so. In fact, the sorting code is only in the function with the comparator. And the one without a comparator calls a function with a comparator with the standard less functor.
But take a look at the description of less:
template &lt;class T&gt; struct less {
  bool operator() (const T& x, const T& y) const {return x&lt;y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

This is a structure whose type must be known to initialize an object.
Those. you need to write:
std::less&lt;тип&gt; comp;
And call:
sort (first, last, comp);
But in this place we do not know the type.
You can use the new C++11 standard and call:
sort( first, last, std::less&lt; decltype(*first) &gt;() );

But how correct is this (not only is it not clear which pointer is being dereferenced, but what about old compilers)? How to write better (more compact, prettier) differently? Just for the sake of interest.
Quite working bubble sort in the above way to have a better idea
#include &lt;algorithm&gt;    // std::swap

class BubbleSort
{
public:

  template&lt; class RandomAccessIterator &gt;
  static void sort( RandomAccessIterator first, RandomAccessIterator last )
  {
    sort( first, last, std::less&lt; decltype(*first) &gt;() );
  }

  template&lt; class RandomAccessIterator, class Compare &gt;
  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 );
      }
    }
  }

private:
  BubbleSort(void);
  ~BubbleSort(void);
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vScherba, 2013-06-25
@nicolausYes

template< class RandomAccessIterator >
void sort( RandomAccessIterator first, RandomAccessIterator last )
{
    sort( first, last, std::less< std::iterator_traits<RandomAccessIterator>::value_type >() );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question