Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-01-20 13:37:50


On Sunday 20 January 2002 01:15 pm, you wrote:

> Specifying what types you want the template to be constrained to could
> be done with a traits class. For example, here's an imaginary syntax
> using a "restrict" keyword:
>
> template <typename NumType :
> restrict(boost::is_arithmetic<NumType>::value)>
> NumType
> foo(NumType t) {...}
>
> People often say: Well if NumType doesn't satisfy the requirements of
> foo, then you will get a compile time error. So what is the problem
> with that?

There are very neat things one could do with constrained genericity. Here's
my favorite (using your "restrict" syntax, though I'd be afraid to propose a
new keyword as well):

template<typename T>
class shared_ptr {
public:
  template<typename To : restrict(boost::is_base_and_derived<To, T>::value)>
  operator shared_ptr<To>();
};

        Doug


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