Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 1999-11-26 11:38:21


Perhaps a twist to automate many of the parameter_traits specializations?

template< typename T, bool IsSmall = sizeof(T) <= sizeof(int) > struct
parameter_traits;

template< typename T >
struct parameter_traits< T, false >
{
        typedef T template_type;
        typedef T raw_type;
        typedef T const & parameter_type;
        static const char * which() { return "non-specialized big template"; }
};

template< typename T >
struct parameter_traits< T, true >
{
        typedef T template_type;
        typedef T raw_type;
        typedef T const parameter_type;
        static const char * which() { return "non-specialized small template"; }
};

I haven't kept pace with which typedef's need to be in there, so don't
read anything into that. I did get John's "saftey const" in there though.

This makes it more complicated to declare a specialization, but you'll
need a lot less of them:

template<>
struct parameter_traits< double, sizeof(double) <= sizeof(int) >
{
        typedef double template_type;
        typedef double raw_type;
        typedef double const parameter_type;
        static const char * which() { return "double specialization"; }
};

And my compiler is currently throwing a fit over:

template< typename T >
struct parameter_traits< T&, sizeof(T&) <= sizeof(int) >
{
        typedef T & template_type;
        typedef T raw_type;
        typedef T & parameter_type;
        static const char * which() { return "T& specialization"; }
};

But this appears to work:

template< typename T >
struct parameter_traits< T&, true >
{
        typedef T & template_type;
        typedef T raw_type;
        typedef T & parameter_type;
        static const char * which() { return "T& specialization"; }
};

I'm not even going to attempt the array specialization.

The extra complication is definitely a down side. But a big win is that
this "does the right thing" for all the container iterators (and most of
the built-in types) without further ado.

If someone could come up with a way to get rid of the extra bool stuff on
the specializations that would be great:

template<>
struct parameter_traits< double > // true or false
{
        typedef double template_type;
        typedef double raw_type;
        typedef double const parameter_type;
        static const char * which() { return "double specialization"; }
};

Just thinking before I send this ... perhaps comparing against
sizeof(void*) would be better than comparing against sizeof(int)? Need
to think about that some more.

-Howard


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