Freestyling on patterns, idioms and semantics…

Icon

vivid hallucinations for bloodthirsty digital vampires

Singleton: a mirage of perfection

There are a very few lucky girls on earth who have had the pleasure to read the great and yet so chemically romantic book “Modern C++ Design: Generic Programming and Design Pattern Applied“. But if you are not one of them, don’t worry: it really does not matter.

In that book there is an entire chapter devoted to present a whirl of singleton patterns that Andrei Alexandrescu gracefully dissects with his fervid insight of the out most boundaries of the C++ language. Andrei presents a unique, portable, policy-based pattern (also implemented in his Loki library) which aims at covering most features required to build a perfect Singleton.

What? A singleton? Uhmkay, let’s rewind the tape.

Read the rest of this entry »

Filed under: programming, ,

All the things you have always wanted to know about writing a generic perfect forwarder

A generic forwarder is a template function supposed to forward the passed arguments to a target function or a callable object (provided for example as an extra argument).

To be perfect a forwarder must be able to be deployed in different contexts, allowing to pass both lvalue and rvalue parameters, yet having the template parameter types correctly deducted and the target function properly chosen from a possible overloaded set.

The current C++ standard does not allow to implement such a perfect forwarder that instead is enabled by means of C++0x rvalue reference. Nevertheless, a generic non-perfect but correct forwarded is possible with the help of tr1::reference_wrapper.

Read the rest of this entry »

Filed under: c++, C++11, programming, ,

Follow me on twitter…