beautypg.com

Using the traits technique – HP Integrity NonStop H-Series User Manual

Page 219

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Using the Traits Technique

To implement a traits parameter for a class, you add it as an extra template parameter to your
class. You then supply a class for this parameter that encapsulates all the specific operations.
Usually that class is itself a template.

As an example, let's look at the matrix problem described above. By using the traits technique,
when you want to add a new type to the matrix you simply specialize the traits class, not the
entire matrix. You do no more work than you have to and retain the ability to use the matrix on
any reasonable number.

Here's how the matrix traits template and specializations for long and int might look. The
example also includes a skeleton of the matrix class that uses the traits template.

template
class matrix_traits
{
// traits functions and literals
};
template
class matrix
{
// matrix
}

class matrix_traits
{
// traits functions and literals specific to long
};

class matrix_traits
{
// traits functions and literals specific to int
};

_ etc.

matrix > int_matrix;

This manual is related to the following products: