More Interesting C++ Notions

I’ve been doing some work in C++ again the past few days, and it is always good to check back on your first computer language even though these days I prefer to be called a Lisper and use Python a lot more. This time, I was checking out the source of the utterly mind-blowing and awesome OpenCV project! While looking at the code for their Support Vector Machine class in the ML module, I noticed 2 interesting ideas and after some background research, here they are:

1. Const Functions

We all know very well about the const keyword in C/C++ and multiple other languages. This keyword helps define constant variables so that we can be assured that our code does not change the value of PI from 3.1415 to 2.71828 (hi-five if you got the hint!). So what exactly are const functions? Functions that don’t change their behavior? Pfft, of course not silly! Functions never change their behavior unless you edited them or your code got hacked because you allowed some form of code injection (I place my bets on stack smashing). In this case, the issue has to do with C++ and its object system. Now when we define an object in C++, we can mark it const to ensure that the object reference of a variable and the object itself is enduring and unchanging. But what is the purpose of methods in a class? They only exist to change the state of an object, which shouldn’t happen if we have marked an object as const. So what we do, is that we mark the functions that don’t change object state as const (similar to static functions in other languages, remember C++ only allows static for variables) so that the compiler knows that it is safe to call this function/method from a const object. Of course, you can always call a const function from a non-const object since this function is guaranteed to not change the invoking object’s state which is alright for a non-const object. The rules can be summarized as:

  1. Const functions can always be called.
  2. Non-const functions can only be called by non-const objects.

2. Typedef for Functions

Admit it, we abuse the typedef keyword. What was added as syntactic sugar to help code readability and shorten elaborate declarations by allowing aliases for keywords is overused by competitive programmers and those without a fundamental sense of code clarity. Hey, I’m not innocent myself, using typedef for an advantage in programming competitions, but the real aesthetic of this keyword is how we use it to improve our code’s accessibility.

Philosophy aside, we have been content to use typedef just to alias our variable declarations, but little do people know that typedef can apply to functions as well.

Take this code for example:

As you can see, the main idea here is that we can define an alias for the Addition function and call it as Add. To typedef a function, we need to declare the alias as a function pointer after the typedef keyword while specifying the parameters and return type as in a normal function signature (line 10). Then all we need to do is map the function pointer to a variable (line 26) and use that variable as a normal function (line 28). Voila! Now you can define easy to remember and map function names for those pesky, over-complicated functions whose names make no sense without touching (or worse, breaking) the original code!

Hope you liked these unique ideas present in C++, since the language receives enough flak for being bulky (something that I am reading upon in a research paper by the legendary John Backus). You can read about my earlier C++ notions here.

Eviva!

Interesting C++ Notions

Hey there!

Been really busy with coding and projects these past few days so no updates. But I have learned a lot of cool new stuff which I now wish to share with you.

Let’s dive in: I am guessing you remember what a stringstream is? If you don’t, there is an old post that I have explaining its super-utility. Once you can get your head wrapped around it, one interesting thing to note is how to clear the stream. On careful scrutiny of the API, you find it has 2 methods, flush() and clear(), which you may think might get the job done. Well, turns out both are quite misleading. The flush() command is actually used to synchronize the stream buffer so that it writes any unwritten characters on the next opportunity it gets, and the clear() command is actually used to reset the flag bits associated with the stream. Thus, none of them achieve our basic objective of clearing the stream.

The answer is actually in the constructor. The str() method is the way to go. Simply call ss.str(“”) and ss.clear() to reset any fail bits (where ss is my stringstream variable), and there you have it.

Another neat trick is that of manipulator functions. We use endl when dealing with cin and cout. Turns out, you’ve been using a manipulator function all along. Other uses include printing decimals in scientific notations and changing the base of your output/input.

Probably the most useful of all the ideas here is the use of a comparator function in the sort function. The template of sort() is sort(start_iterator, end_iterator, comparator_func) and we don’t usually use this form simply because of the awesome overloaded capabilities of the sort() function. However, this form gives us greater control over our execution and I have personally used innumerable times.

The comparator function takes 2 parameters of the same type as the list to be sorted. We return true if the first parameter goes before the second and false otherwise. Thus, this allows us to define the comparison criteria for user types like classes and structs. 😀

A final trick that I would like to mention is the transform function. A handy tool for performing various transformative operations on lists, the most basic being changing the case of strings. This one needs some memory to remember the manipulator functions, but I am sure you can handle it.

Hope this helps you write more succinct, poignant code in the future.

Till then, eviva!