8. Class Templates
- Rec. 39
- Do not attempt to create an instance
of a class template using a type that does not define the member functions
which the class template, according to its documentation, requires.
- Rec. 40
- Take care to avoid multiple definition
of overloaded functions in conjunction with the instantiation of a
class template.
It is not possible in C++ to specify requirements for type arguments
for class templates and function templates. This may imply that the
type chosen by the user, does not comply with the interface as required
by the template. For example, a class template may require that a type
argument have a comparison operator defined.
Another problem with type templates can arise for overloaded
functions. If a function is overload, there may be a conflict if the
element type appears explicitly in one of these. After instantiation,
there may be two functions which, for example, have the type
int as an argument. The compiler may complain about this,
but there is a risk that the designer of the class does not notice it. In
cases where there is a risk for multiple definition of member functions,
this must be carefully documented.
Example 34: Problem when using parameterized types (Cfront 3.0 or other template compiler)
template <class ET>
class Conflict
{
public:
void foo( int a );
void foo( ET a ); // What if ET is an int or another integral type?
// The compiler will discover this, but ...
};
|