3

As pointed out by this post, in 'Modern C++ Design - Generic Programming and Design Patterns Applied', Andrei Alexandrescu uses classes with template template parameters to implement the policy pattern:

// Library code
template <template <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>
{
   ...
};

Now, suppose that the policy class has a state (i.e. it has non-static members) and that the host class stores an object of the policy class:

// Library code
template <template <class> class Logger>
struct Algorithm
{
   Algorithm(Logger<Event> &logger) : logger_(logger) {}
   ...
   private:
       Logger<Event> &logger_; 
};

The user will probably write something like this:

using MyLogger = ConcreteLogger<MyEvent>;
MyLogger log;
Algorithm<ConcreteLogger> alg(log);

Since the user needs to instantiate ConcreteLogger, is there any remaining advantage in using template template parameter in this case?

Community
  • 1
  • 1
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • 2
    Template template parameters usually aren't as useful as type parameters, as you just discovered. Typical examples for using type parameters to pass policy *objects* (not types) can be found in the standard library, namely in the way predicates are passed to algorithms, and allocators to containers. – Kerrek SB Dec 07 '15 at 09:55
  • @KerrekSB Thank you for the confirmation. – AlwaysLearning Dec 07 '15 at 10:29

0 Answers0