|
Boost : |
From: Michael Fawcett (michael.fawcett_at_[hidden])
Date: 2008-01-10 16:57:05
On Jan 10, 2008 4:43 PM, Doug Gregor <dgregor_at_[hidden]> 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.
This has always been my thinking, but to play devil's advocate, what
about providing an adapter class around that type that you can't
modify? To answer my own question, it provides a much larger burden
on the user of your library (wrap all instances with wrapper type when
using my generic algorithms vs. specializing one traits struct).
>From the following possible (contrived) implementations of retrieving
data from a Node, which would be best in your opinion (where Node
Concept is basically some pointers to other Nodes plus data, like a
linked list node, or tree node)?
// Only Concept
template <typename T>
typename T::data_type *data(T *node)
{
boost::function_requires<NodeConcept>();
return node->data;
}
// Partial traits
template <typename T>
typename node_traits<T>::data_type *data(T *node)
{
boost::function_requires<NodeConcept>();
return node->data;
}
// Completely traits
template <typename T>
typename node_traits<T>::data_type *data(T *node)
{
boost::function_requires<NodeConcept>();
return slist_node_traits<T>::data(node)
}
and, if you suggest using all traits, would Node Concept be reduced to
simply checking to see if node_traits was fully specialized?
--Michael Fawcett
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk