Registering template types
To one type in the C++ type system must correspond one type interface. As you know, class templates are not considered as C++ types, only instances of these templates form types.
It is sometimes needed to register these class templates to the type system, so that all instances of these class templates are registered the same way in the type system. The type system also allows to check if a certain type is an instance of a given class template.
Only templates with one argument are supported, though this should be expandable with C++11 variadics.
This feature is used for
for example which is registered as an
How it works ¶
The class from which the type interfaces inherit is
on the class template you want to register. For
. This class is
and not defined.
, one must inherit from the type interface
they want to implement. For example, in
, they are described in
Object type erasure
must define the function
which returns a
. This function is likely to be virtual because you don’t know
yet the type of the template argument.
Then you must specialize
which takes two template
arguments. The first is the class template (
in our case) and the
second is the template argument given to it (usually
). You specialize this
class over the first argument only.
Finally, you must specialize the
class, as explained in
How types are registered
. You can do that through the
macro, declared in
Particular case of Objects ¶
For objects, like
, you must advertise all the methods of your object.
This is done through a builder, but since the class inherits from the type
interface itself, you can’t use the type interface provided by the builder.
The small difference there is that you must not use
which is a function of your
Retrieving the template type ¶
It is then possible to retrieve the template through a
type interface. You can use the macro
, defined in
to do that easily.
class may be useless, it seems possible to skip it and