cplusplus.co.il

Archive for the ‘techniques’ Category

Imitation of Java Generics

Posted by: rmn on: 31/12/2010

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 [...]

Overloading macros

Posted by: rmn on: 31/08/2010

The feature of function overloading can prove to be pretty useful: it allows us to define a few versions of the same function, which differ in argument types, or even in Arity (ignoring variadic functions for the moment). Unfortunately, the C\C++ pre-processor does not allow overloading macros in the same way; It treats such attempts [...]

Variadic macro to count number of arguments

Posted by: rmn on: 17/07/2010

The C-Preprocessor is a very powerful mechanism, which offers many different features. One of these features is called Variadic macros: macros that accept a varying number of arguments. It is interesting to note at this point, that such Variadic macros, despite being part of the C99 Standard, are not part of the C++ Standard at [...]

Memoization

Posted by: rmn on: 07/01/2010

Memoization is essentially a fancy name for caching results for future use. A generalization of dynamic programming, if you will. While I am certain most of us use it one way or another, in many occasions, it is usually through an Ad hoc implementation.. One that is only suitable for the specific, current, use case. [...]

Tag dispatching

Posted by: rmn on: 03/01/2010

Tag dispatching is a technique for compile time dispatching between a few overloaded functions by using the properties of a type. This technique usually involves some kind of type traits.

Many times we are required to define both a copy constructor and an assignment operator (for example: according to the rule of three, if we need one we are likely to need the other). The two will probably share a pretty common code, if not exactly the same. So what we will do in many [...]

Computations at compile time

Posted by: rmn on: 13/08/2009

The C++ template mechanism is a very powerful tool. Besides its great ability of code generation, it can also be used to make useful computations at compile time. Let us introduce such an example.


Follow

Get every new post delivered to your Inbox.

Join 25 other followers