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;
  a.push_back(1);
  a.push_back(2);
  std::sort(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:

  template
  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.


2 comments:

  1. I had the same issues with C++, but with networking, so I did the same and wrote a library: https://github.com/nathanpc/libinet

    C++ is a great language, but it still lacks some modern things.

    ReplyDelete
  2. Exactly, we need libraries which humans can use. It's one of the fields where Java beats C++ hands down. C++11 is promising, but just if the STL was made more human-friendly, we have a long way.

    Btw, your sockets library looks quite friendly, will give it a shot when i need one!

    ReplyDelete