Saturday, March 2, 2013

C++: Why can't I sort like a normal human being

Before I begin, C++ is awesome.
It's exactly the right kind of balance between low and high level languages. When you need C++, you need C++.

But it comes with it's own sets of problems.
The Standard Template Library (STL) shipped with C++ is quite great, but at the same time it's excruciating verbose and is very counter intuitive.

For instance, std::sort sorts C++ the elements in a container in place. Sounds good enough.
Now you would expect to sort like this:

  vector<int> a;

But, NO.
std::sort would only sort over a range of elements over a container, so you must write:

  std::sort(a.begin(), a.end());

This really irritates me.
std::sort(a) would have been brilliant, easy to use, and intuitive.
I tried to pull up a simple overload and worked like a charm:

  void sort(T& t)
      return std::sort(t.begin(), t.end());

Its one of the things that bug me as a C++ developer and HCI enthusiast.
Not overloading enough to provide simpler interfaces to your end users is simply evil and mean.