Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2000-10-10 14:00:31


Daryle Walker wrote on 10/10/2000 1:55 PM
>I haven't written anything for this yet, but I was thinking of making a
>certain kind of a numeric adapter class. It should support all the
>arithmetic operations the base type supports.
>
>The problem is the modulus operators. They should be defined only if the
>base type is integral. I can check this from the base type's
>std::numeric_limits implementation. The value should be a compile-time
>constant, so I can use it as a template parameter.
>
>So I could make one version of my adapter class that includes modulus
>operations, and one that doesn't. But I want the end user to see only one
>adapter class, which would secretly inherit(?) from one of the specialized
>adapter classes (based on the numeric-limit compile-time constant). How
>would I do it, or do I have to settle for a single adapter class without
>modulus operations?
>
>(I know this technique can work for template functions. For example, the
>iterator std::distance function could specialize for different iterator
>types by secretly calling another function that takes the iterator's type
>tag as an extra argument. I'm asking for a similar technique for template
>classes.)

Maybe something like:

template <class T, bool IsIntegral = std::numeric_limits<T>::is_integer>
class numeric_adapter
     : private numeric_adapter_imp<T>
{
     // IsIntegral == false
     // forward to base class
};

template <class T>
class numeric_adapter<T, true>
     : private numeric_adapter_imp<T>
{
     // IsIntegral == true
     // forward to base class
     // Add support for %
};

Disadvantage with this design is that the default template parameter
won't be invisible with template templates. Compiler must also support
partial template specialization. I'll leave it to the MS specialists to
remind you how to work around that if you go this route.

-Howard


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