1.4.2. Formatter

Another frequent programming task is generating information for the user. Often you have to assemble a string from several parts. Most of the time there is some sort of format string to determine the basic information with some placeholders at the desired position which are substituted with actual values.

A lot of people are using the printf() family for outputting. Unfortunately these are not typesafe. And nor are they appropriate for translations when the positions of the substituted parameters are swaped for grammatical reasons.

So I decided to implement a typesafe approach which also takes into account the position of the substituted values: each inserted value is converted into its string representation and substitutes the placeholder with the lowest number. Since such a placeholder consists of a percent sign and a single digit there are up to ten substitutions possible.

The built-in methods handle the common data types like int, float or std::string. But it is also easily possible to extend the ouput capabilities for your own data types. No changes inside the mockpp framework are needed, it needn't even be in the mockpp namespace.

In the case you don't really want to output the content of an object but simply need operator<<() because mockpp insists on it for it's built-in debugging output you may use the default templates instead. To avoid pontential conflicts these templates must be enabled by setting the macro MOCKPP_ENABLE_DEFAULT_FORMATTER before including the first mockpp header file.

This collection of functions is inspired by the QString class which is part of the Qt™ framework by Trolltech.

  class Person 1

      Person(std::string in_name, unsigned in_age)
        : name(in_name)
        , age(n_age)

      String toString() const
        String ret;
        ret << name << "(" << age << ")";
        return ret;


      std::string  name;
      unsigned     age;

  String & operator<< (String &formatter, const Person &pers)
     formatter << pers.toString(); 2
     return formatter;

  Person pers("Bob", 6);
  String  format = "%4 says: %3 plus %2 gives %1";
  format << "three" << "two" << "one" << pers;
  std::cout << format << std::endl;


Create a user defined data type with some internal data elements. Implement a method to create a string representation of the internal variables.


Output the string representation of the object.

The resulting output: Bob(6) says: one plus two gives three