Boost logo

Boost :

From: Tanton Gibbs (thgibbs_at_[hidden])
Date: 2002-08-02 12:26:50


I was recently wondering the correct way to allow the user to overload some
policy decisions and ignore others. One solution that was presented earlier
on this list involved named template parameters. The solution I thought of
involves a Default class. I was wondering if anyone has used this solution
before and what their experiences were with it. Basically, you create a
Default class and allow the user to pass it in anywhere he or she would like
the Default, then the actual class can use the MPL select_if algorithm to
replace Default with the correct class.

For example,

namespace mpl = boost::mpl;

class Default {};

template< class ThreadPolicy = SingleThreaded,
                class PointerPolicy = BarePointer >
class Foo {
  typedef mpl::select_if_c< boost::is_same<ThreadPolicy, Default>::value ==
1, SingleThreaded, ThreadPolicy>::type ThreadPolicyType;
  typedef mpl::select_if_c< boost::is_same<PointerPolicy, Default>::value ==
1, BarePointer, PointerPolicy >::type PointerPolicyType;
  public:
    void DoIt() { ... }
};

Then the user's code could be

Foo<Default, SmtPointer> f;

I think this provides an easy way to specify arbitrary parameters, but I
would like to get others insight on the matter.

Thanks!
Tanton


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