I\'ve seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses do
Say you're using CRTP to provide an "interface" for a set of child templates; and both the parent and the child are parametric in other template argument(s):
template class interface {
void do_something(VALUE v) {
static_cast(this)->do_something(v);
}
};
template class derived : public interface {
void do_something(VALUE v) { ... }
};
typedef interface, int> derived_t;
Note the duplication of 'int', which is actually the same type parameter specified to both templates. You can use a template template for DERIVED to avoid this duplication:
template class DERIVED, typename VALUE> class interface {
void do_something(VALUE v) {
static_cast*>(this)->do_something(v);
}
};
template class derived : public interface {
void do_something(VALUE v) { ... }
};
typedef interface derived_t;
Note that you are eliminating directly providing the other template parameter(s) to the derived template; the "interface" still receives them.
This also lets you build up typedefs in the "interface" that depend on the type parameters, which will be accessible from the derived template.
The above typedef doesn't work because you can't typedef to an unspecified template. This works, however (and C++11 has native support for template typedefs):
template
struct derived_interface_type {
typedef typename interface type;
};
typedef typename derived_interface_type::type derived_t;
You need one derived_interface_type for each instantiation of the derived template unfortunately, unless there's another trick I haven't learned yet.