Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2002-05-16 10:36:05


> I often need when manipulating elementary types (char, unsigned char)
> that I want a conversion such as:
>
> boost::add_unsigned<T>::type returns the unsigned integral type,
> for builtin integral types (bool,
> char, short, int, long int, and
> their variants), and T otherwise
>
> boost::add_signed<T>::type returns the signed integral type,
> for builtin integral types (bool,
> char, short, int, long int, and
> their variants), and T otherwise

I'm not opposed to adding these, but you don't need them for your example:

> often, I need
> to keep a table for the range of possible values of the type. For
> instance, in counting_sort<char>, you need counter[i] where i is in
> [-128, 127]. (Another example is in programming compression filters
> like huffman codes, etc.)
>
...
>
> 2. have a traits class with a type and a member:
> class unsigned_traits {
> typedef boost::add_unsigned<T> index_type;
> static index_type(T x) {
> return index_type(x - boost::integer_traits<T>::const_min());
> }
> };

If your algorithm uses an actual array, use std::size_t for the
"index_type"; if it's using a container, use "Container::size_type". Both
of these are unsigned, and your result from biasing the index will be
converted to this type anyway. You can pass this index_type as a second
parameter to unsigned_traits if you wish.

Unless I misunderstood something...

        -Steve


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