Boost logo

Boost :

From: Herve Bronnimann (hbr_at_[hidden])
Date: 2002-05-16 09:39:43


Hi Boosters:

Here's a request for a frequently needed type_traits-like feature, as
well as comments on a generally needed structure.

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 don't know if it makes sense, but here's why I need it: 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.)

There are two ways I can think of to do that:

 1. let counter = table[0] + 128, so that counter[i] is always
    a valid pointer

 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());
        }
      };

I prefer 2 as 1 is error-prone, but I find I am reprogramming 2. over
and over again, and especially the add_unsigned<T> part... which is not
portable anyway, for compilers who don't have partial specialization.

If you have any idea of how to do it nicely with existing boost parts,
let me know.

-- 
Hervé

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