sunny
sunny

Reputation: 3891

Create a policy template class with a policy template class factory?

I would like to create a templated class factory method that in turn creates a templated class itself inheriting from policy templates. Here is what I've tried for the factory method:

template <typename T, template <class Created> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget<T>>
{
public:
  WidgetManager() {};
  static Widget<T>* doAll(){
    Widget<T> *t = WidgetManager::Create(); //the static create method comes from CreationPolicy
    return t;
  }
};

But the Widget I would like to make should itself inherit from a template policy class:

template<template <class Created> class IntPolicy>
class Widget : public IntPolicy<Widget>
{};

with IntPolicy like so:

template <class T>
struct BigInt
{
  int LOGNOW = 99;
};

template <class T>
struct SmallInt
{
  int LOGNOW = 5;
};

What's the proper syntax to do this? Everything I have tried gives me type errors. For example, the above gives "template argument 1 is invalid" or "type/value mismatch at argument 1 in template parameter list for <template <template<class Created> class IntPolicy> class Widget

Upvotes: 0

Views: 231

Answers (1)

Daniel Jour
Daniel Jour

Reputation: 16156

I guess thinking in terms of type level functions helps:

Widget needs a template which requires 1 type to become itself a complete type:

Widget :: (* -> *) -> *

So whatever T in Widget<T> is, it needs to be a template accepting one complete type (* -> *). But what is T in your case? A typename T (*). You cannot use the template BigInt for that.

So I guess changing that typename T to a template <class> class T should do the trick.

Upvotes: 1

Related Questions