Modern C++ Design: Learn with Loki Library

If you decide to start learning the modern C++ design and you come from the OOP school, you can start by looking inside the loki library.

Loki is the name of a C++ software library written by Andrei Alexandrescu as part of his book Modern C++ Design.

The library makes extensive use of C++ template metaprogramming and implements several commonly used tools: typelist, functor, singleton, smart pointer, object factory, visitor and multimethods.

And here’s the author’s motivation behind the design of this library

  • Loki users only have to pay for the features they want. Each and every of Loki’s components can be used in isolation from the others.
  • “Small is beautiful” – Loki’s internal dependencies are small.
  • “Multiplicative is splendid.” – Loki’s particular focus is on obtaining multiplicative behaviors (i.e., fine-grained, specialized designs), by combining small, abstract bits of behavior (policies).
  • Loki focus on strategy not tactics. Design, not bits. Architectural components, not the kitchen sink.
  • As Loki is a library of designs and not the design of a library, it should make minimum assumptions about its environment and provide appropriate hooks wherever some default decision is chosen.
  • Loki doesn’t cater for lesser compilers. Loki aims at writing readable code within the realm of standard C++.
  • The components ideally are small, easy to understand and use correctly, useful either in isolation or together, and of high impact in projects.

But before going deep inside its modern C++ idioms, it’s better to master a central piece of the template programming, it’s the policy based design idiom.

The central idiom in policy-based design is a class template (called the host class), taking several type parameters as input, which are instantiated with types selected by the user (called policy classes), each implementing a particular implicit interface (called a policy), and encapsulating some orthogonal (or mostly orthogonal) aspect of the behavior of the instantiated host class. By supplying a host class combined with a set of different, canned implementations for each policy, a library or module can support an exponential number of different behavior combinations, resolved at compile time, and selected by mixing and matching the different supplied policy classes in the instantiation of the host class template.

Here’s a mini sample from Wikipedia to demonstrate the use of this useful idiom

#include 
#include 
 
template 
class HelloWorld : private OutputPolicy, private LanguagePolicy
{
    using OutputPolicy::print;
    using LanguagePolicy::message;
 
public:
    // Behaviour method
    void run() const
    {
        // Two policy methods
        print(message());
    }
};
 
class OutputPolicyWriteToCout
{
protected:
    template
    void print(MessageType const &message) const
    {
        std::cout << message << std::endl;
    }
};
 
class LanguagePolicyEnglish
{
protected:
    std::string message() const
    {
        return "Hello, World!";
    }
};
 
class LanguagePolicyGerman
{
protected:
    std::string message() const
    {
        return "Hallo Welt!";
    }
};
 
int main()
{
    /* Example 1 */
    typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyEnglish> HelloWorldEnglish;
 
    HelloWorldEnglish hello_world;
    hello_world.run(); // prints "Hello, World!"
 
    /* Example 2 
     * Does the same, but uses another language policy */
    typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyGerman> HelloWorldGerman;
 
    HelloWorldGerman hello_world2;
    hello_world2.run(); // prints "Hallo Welt!"
}

After discovering the policy based design you can move to the other idioms used by the library:

  • Multiple dispatcher
  • Pimpl
  • Printf (a typesafe printf replacement)
  • Ordered static object creation
  • Scope guard pointer
  • Small Object Allocator
  • Smart pointer
  • Compile time check
To resume the loki library is not popular ast STL or Boost, but it’s an interesting educational library to learn the basic modern c++ design idioms. It’s a good start to master the modern C++ design.