|
Boost : |
From: Andrei Alexandrescu (andrewalex_at_[hidden])
Date: 2003-08-04 18:38:01
"John Madsen" <johnmadsen_usenet_at_[hidden]> wrote in message
news:bfie34$hb4$1_at_main.gmane.org...
> >- A policy based design to whereas the policy extends the classes
interface.
> >- Stronger typing (the types are different based on typename, not on
> >traits). The traits approach seems fundamentally flawed as two separate
> >types could share the same traits.
> >
>
> I think that the traits system makes the typing stronger. It guarantees
> distinct types even in the face of handles that are otherwise
> indistinguishable. I looked at your design, but I think I can handle what
> you're trying to do through traits class inheritance. I don't see any use
to
> extending the interface through a policy class. Let me know if I've
missed
> something.
I might not have followed the discussion to deeply, but it does look to me
like John is entirely right. Traits can fundamentally do one customization
per type. That's not going to be enough if you have the same type
representing multiple handles, as is the case with many C APIs. For example,
sockets and file descriptors might be both integers. Or, a variety of
handles can come in the disguise of a void*. In Windows, if you #define
STRICT, they use a trick to give different flavors of HANDLEs different C++
types (nothing fancy, it's all casts). Still, they forgot to do that for a
couple of handle types (which were that? HBITMAP? HINTERNET? I can only say
HGEEITSBEENALONGTIME). So then after a team I was working with built a very
nice and sweet "smart handle" class using traits, we simply couldn't use it
because of this particular issue.
With a trait, you can only establish one way of dealing with int and one way
of dealing with void*. With a policy, you can define at the exact
granularity that you want how exactly your handle maps to a type and how it
is manipulated - and that policy will be part of the object type. I'm not
sure that the inheritance-based approach is superior or not, it sure would
be worth some discussion.
For more reference on traits, here are two articles that might be of
relevance:
http://www.moderncppdesign.com/publications/traits.html
http://www.moderncppdesign.com/publications/traits_on_steroids.html
Andrei
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk