Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2003-01-11 11:53:25


On Saturday, January 11, 2003, at 09:42 AM, Beman Dawes wrote:

> >Or maybe even just:
> >
> > template <class T>
> > struct my_container
> > : if_<is_POD<T>::value, impl1, impl2>::type
> > {
> > ...
> > };
>
> These are the examples that resonate with me, particularly Howard's
> version. It looks so easy, and has obvious practical uses. It would
> motivate me.
>
> But it needs to be a complete, compilable, runable, program so I can
> try it, modify it, etc.

Despite the fact that I suggested it, I don't find it a killer example.
;-)

I already code stuff like this, with this other pattern:

template <class T, bool> struct impl;

template<class T> struct impl<T, true> { }; // impl1, optimized

template<class T> struct impl<T, false> { }; // impl2, not optimized

   template <class T>
   struct my_container
      : private impl<T, is_POD<T>::value>
   {
      ...
   };

I'm not convinced that the version using if_ is significantly simpler.
:-\

template<class T> struct impl1 { }; // optimized

template<class T> struct impl2 { }; // not optimized

   template <class T>
   struct my_container
      : private if_<is_POD<T>, impl1<T>, impl2<T> >::type
   {
      ...
   };

if_ really shines when it is inconvenient to partially specialize an
entire class just to get one little different type:

template <class T>
struct some_example
{
        typedef typename if_<some_condition<T>::value, type1, type2>::type
the_type;
        ...
};

Good example:

http://www.cuj.com/experts/1901/austern.htm?topic=experts

-Howard


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