Generic programming is very common and has different names (and features) in various programming languages; C++ provides Turing-Complete Templates, Java offers Generics (along with Ada, Eiffel, C#, and Visual Basic .Net), and Parametric Polymorphism is present in ML, Scala, and Haskell.
While I am generally pretty happy with what C++ has to offer, adopting some of the features offered by other mechanisms can come in handy sometimes. To be more specific, in this post we will mimic Java’s support for defining a common super-type for generic elements (i.e. List).
Continue reading Imitation of Java Generics
In the concurrent world, a Future object refers to an object whose actual value is to be computed in the future. You can think of it as a handle to an asynchronous invocation of a computation that yields a value.
Many so called concurrent programming languages support this idea as a native construct offered by the core language itself. Unfortunately, C++ does not. Well, at least not in the current standard; C++0x (or shall I say C++1x ?) is going to support std::future as part of the massive new C++0x thread library, which is based on boost::thread. In this post we will implement a simple, yet very powerful, such future object.
Continue reading Futures: asynchronous invocation
The possibility of overloading just about any C++ operator and having it do something entirely different from what it was designed for, can sometimes make life pretty hard.
Here are a couple of examples: What if you wanted to take the address of an object, which had implemented an entirely different semantic for the ampersand (&) operator? Or what if somebody decided to overload the comma operator in some strange manner?
As you could have guessed, there are solutions for such scenarios.
Continue reading Escaping overloaded operators
The powerful template mechanism of C++ allows us to write pretty complex Meta Functions, which are executed by the compiler during compilation. There are two basic types of meta-functions: one whose result is a type (mainly dealt with by Boost.MPL), and the other is a compile-time computation (which can result in any compile time constant). In this post we will review an example of the latter.
We would like to achieve a compile time boolean constant of the form is_prime::res. The problem with such meta programming task, in my opinion, is that it requires a functional programming mindset. Something that we, as C++ programmers, aren’t necessarily used to. But I am sure we will be able to tackle it anyway.
Continue reading Compile time primality test
One of the containers introduced within TR1 (which is already widely available – both in gcc and Visual Studio) is a Tuple type, which is adopted from The Boost Tuple Library. Tuple types are very convenient at times; For example, it is possible to return multiple values from a function through a tuple, or write more intuitive and expressive code by utilizing tuples.
In this post we will examine the functionality offered by the new Tuple container, and have a go at profiling its performance. Actually, the results of said profiling were a small (pleasant) surprise to me.
Continue reading Tuples