Boost logo

Boost :

From: Douglas Gregor (dgregor_at_[hidden])
Date: 2008-01-11 08:40:33


On Fri, 2008-01-11 at 12:25 +0100, Tobias Schwinger wrote:
> Doug Gregor wrote:
> > On Jan 10, 2008, at 3:40 PM, Michael Fawcett wrote:
> >> My questions are - when creating your own library, when do you choose
> >> nested typedefs over a traits class,
> >
> > Never.
> >
> >> and what effects does it have on
> >> a library user, both from a usability and extensibility point of view.
> >> What questions do you ask yourself to come to a decision?
> >
> >
> > Traits classes are far better than nested typedefs, because one can
> > always specialize a traits class for an existing type---even if you
> > can't modify the type because it is built-in or comes from another
> > library that you can't modify.
> >
>
> Type members are inherited by derived classes -- traits specializations
> are not. So I don't think it's generally true that either one is better
> than the other -- it depends on what you're trying to achieve.

>From the standpoint of a generic algorithm, the use of traits allows the
widest variety of existing data types to work with the generic algorithm
without modifying the data types themselves.

Besides, it's typical for the primary templates of traits to fall back
to nested types, which solves the ease-of-use problem for derived
classes (and for most classes, in fact), while still giving the
flexibility of traits, e.g.,

  template<typename Iter>
  struct iterator_traits {
    typedef typename Iter::iterator_category iterator_category;
    typedef typename Iter::value_type value_type;
    typedef typename Iter::reference reference;
    typedef typename Iter::pointer pointer;
    typedef typename Iter::difference_type difference_type;
  };

  - Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk