A technique used in
C++ for reducing the number of
parameters to a given
template.
If a template you are creating has a large number of
parameters (say, more than three), you might want to declare the template like this:
template <class FIRST, class SECOND, class THIRD, class FOURTH>
class foo
{
typedef FIRST first_type;
typedef SECOND second_type;
typedef THIRD third_type;
third_type fourth_func (first_type const &first, second_type cont &second)
{
if (first != 0) return FOURTH(second); else return 0;
}
};
If you do, you are making problems for yourself. Given:
template <class F, class I>
class Floor
{
I operator(F const &f) { return floor (f); }
}
You will have to refer to a
foo<char,float,int,Floor<float, int> > everywhere in your code.
If you do this instead:
struct foo_traits1
{
typedef char first_type;
typedef float second_type;
typedef int third_type;
typedef Floor <float, int> FOURTH;
};
template <class TRAITS>
class foo
{
typedef typename TRAITS::first_type first_type;
typedef typename TRAITS::second_type second_type;
typedef typename TRAITS::third_type third_type;
third_type fourth_func (first_type const &first, second_type cont &second)
{
if (first != 0) return TRAITS::FOURTH(second); else return 0;
}
};
you can now refer to a
foo<foo_traits1> class everywhere you need that particular foo in the code. This will make maintaining and debugging your code easier. If you have a second implementation, you would declare another traits type (
foo_traits2?) and put its name in the angle brackets.
The best-known traits type is the
char_traits<> template used for implementing standard
strings and
iostreams.