Hi,
Is it possible to force the typename of a template to inherit from a
particular class? For example, something like this (conceptual; this
doesn't compile):
class Parent {
public:
[...]
virtual void vfn() = 0;
};
template <child-of-Parent T>
class CC {
[...]
void foo() {
vfn();
}
};
In other words, can I force T to be a child of Parent?
Why do you want to restrict what other people can do with your
template? That's their business.
The fact is that your template can be used in any situation which will
satisfy the generated call vfn() in the foo() function.
But I suspect you may be looking for a "mixin" template:
template <class T>
class CC : public T {
// ...
void foo { vfn(); }
}
Now T has to be a class, because it's used by inheritance. If the class
T has a function U vfn() where U is some type, possibly void, that
function will be inherited and used to satisfy the vfn() call.
T can be anything which has a vfn() function. If you have a non-member
vfn() function in scope, that can be used too! To prevent the use of a
file scope function, and insist on it coming from T, I think you can do
this:
void foo { T::vfn(); }
Beyond that, there is no need to restrict T to be a particular class.