Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-10-23 11:52:41


I think we're not connecting, but I'm not sure where the problem is.

On Fri, Oct 24, 2003 at 12:32:58AM +0800, Joel de Guzman wrote:
> Brian McNamara <lorgon_at_[hidden]> wrote:
> > The general strategy I tend to use for "enable_if-style
> > meta-predicates" is
> >
> > - write a meta-function
> > is_XXX<T>
> >
> > - have an empty base class which serves as the tag name
> > struct XXX {};
> >
> > - have instances of XXX inherit the base class
> > struct foo : public XXX { ... };
> >
> > - have the default implementation of the metafunction use
> > is_base_and_derived to do the test
> >
> > Is this bad? Is there a better way? I have been using this strategy
> > because
>
> If I have control over all classes, I'd defninitely choose that approach.
> There are cases where you don't. For example, we can't force users
> to subclass from a base functor class, it has to be a free F type.

But this approach doesn't force this. You can specialize is_XXX<T> for
your type. The intrusive, inheritance-based approach is simply provided
to afford a convenient default implementation. This is because I think
it's easier for me to write

   struct foo : public XXX { ... };

rather than

   struct foo { ... };
   template <> class is_XXX< foo > { /* something more verbose */ };

or

   struct foo { ... };
   template <> class is_XXX< foo > : public /*something*/ {};

Of course, for a 3rd party class "bar", we can always revert to the
specialization approach to say that "bar is-a XXX". But for classes I
create, I can use the more syntactically-lightweight inheritance
approach. In other words, the two styles are complimentary, rather than
exclusive.

Yes/no/maybe?

-- 
-Brian McNamara (lorgon_at_[hidden])

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