Listing 1: Providing a user-definable policy via a macro

//
//---- begin file library.h ----
//

#ifndef CLASS_POLICY
#  define CLASS_POLICY "default policy"
#endif                  

//
//---- begin file container.h ----
//

#include "library.h"
#include <string>
#include <iostream>


template <class ElementType>
class MyContainer {
public:
   MyContainer(const std::string iName) : instanceName(iName) 
   {}
   // class implementation
   // ...
   
   // performs the policy
   void doPolicy();
   // a service that uses the policy
   void performService() { doPolicy(); }
   
   const std::string instanceName;
};

// elements for example's sake
template<class T>
struct Element1 {
// ...
};

template<class T>
struct Element2 {
// ...
};


template <class ElementType>
void MyContainer<ElementType>::doPolicy() 
{ 
    std::cout << "Policy is: " << CLASS_POLICY << std::endl;
}


//
//---- begin file user.cpp ----
//

#include "container.h"

int main() {
    
   MyContainer<Element1<int> > c("object1");
   c.performService();

   return 0;
}
— End of Listing —