Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-04-02 14:54:29


From: Robert Bell <belvis_at_[hidden]>
> Andy Little wrote:
> > "David Abrahams" <dave_at_[hidden]> wrote in message
> > news:uptaqlaqz.fsf_at_boost-consulting.com...
> >>
> > Surely a trait is *part of the *definition* * of an *entity* in a wider
> > sense.
> > (perhaps *definition* has wrong connotations)
>
> Perhaps. It seems that the crucial distinction is that an entity's
> definition is complete without the trait; traits are not part of the
> definition of an entity, they add on to what the program can know (or
> do) with the entity.

I was bothered by it, too, but I didn't reply before now. You're
right. The traits augment a type with additional information
needed in a context beyond the context in which it was created.

> Using phrases like "a trait is part of the definition of an entity"
> confuses the issue because of the use of the word "definition".

Yeah. I agree.

> For myself, I'm leaning towards the "xxx is a traits or a policy
> depending on how it's used" viewpoint. I find the
>
> template<typename T, typename traits_or_policy = traits<T> >
> class Foo {
> };
>
> example pretty compelling. Whether or not this is an example of good
> design is a red herring; because this idiom can be misused does not
> affect the point that whether traits_or_policy is used as a traits or
> policy class depends on how it's used by the client of Foo.

Whoa! I think you've taken the notion past what we've been
discussing. "traits" in your example isn't a traits class or
policy class depending upon how its used. If the name is to be
believed, "traits" is a traits class because it associates stuff
with T. However, within Foo, the template parameter
traits_or_policy is a policy class.

Let me illustrate:

template<typename T, typename policy = traits<T> >
class Foo {
   Foo() { policy::foo(traits<T>::bar); }
};

traits<T> is both a traits and a policy class within Foo. The
ctor unconditionally uses traits<T>::bar to get something
associated with T. That isn't affected by the policy class given
as the second template parameter. By contrast, the ctor uses the
policy class when it calls policy::foo(). That the second
template argument happens to be traits<T> is immaterial. It
could be another class, thus affecting the ctor's behavior.

(A traits class isn't restricted from having static member
functions and a policy class isn't restricted from having static
data members, I just haven't shown them in this example.)

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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