Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

17.1 Defining the Problem

The traits technique discussed in this chapter is useful for solving the following kind of problem. Consider that you have a matrix that must work for all types of numbers, but the behavior of the matrix depends on the type of number in at least some measure. This means your matrix can't handle all numbers the same way.

Except for the behavioral difference, it sounds like the perfect problem for a template. But you can't use a single template, since you can't hang extra information on the number type because it's often just a built-in type. The template will do the same thing for every number type, which is just what you can't do in this case. You could specialize, but then you have to re-implement the entire matrix class for every type of number. It may well be that most of the class is the same. Worse yet, if you want to leave your interface open for use with some unknown future type, you're requiring that future user to reimplement the entire class as well.

What you really want is to put everything that doesn't change in one place, and repeatedly specify only the small part that does change with the type. The technique for doing this is generally referred to as using a traits parameter.



Previous fileTop of DocumentContentsIndex pageNext file