Boost logo

Boost Users :

From: Niels Dekker - mail address until 2007-11-30 (nd_mail_address_valid_until_2007-11-30_at_[hidden])
Date: 2007-06-08 13:57:52


Scott McMurray (me22) wrote:
>> Given the fact that <cstdint.hpp> is part of Boost already, it would
>> make sense to me to allow easy access to its integer types by means
>> of a templated struct like int_exact.
>>
> Agreed.

Thanks :-)

> I suppose there might be value in making them meta-functions, with
> just ::type

Okay, so instead we could have a templated struct int_exact<Bits>,
containing just one type, taken from <boost/cstdint.hpp>:

    template <unsigned Bits> struct int_exact { };
    template <> struct int_exact<8> { typedef int8_t type; };
    template <> struct int_exact<16> { typedef int16_t type; };
    template <> struct int_exact<32> { typedef int32_t type; };
  #ifndef BOOST_NO_INT64_T
    template <> struct int_exact<64> { typedef int64_t type; };
  #endif

and a similar struct uint_exact:

    template <unsigned Bits> struct uint_exact { };
    template <> struct uint_exact<8> { typedef uint8_t type; };
    template <> struct uint_exact<16> { typedef uint16_t type; };
    template <> struct uint_exact<32> { typedef uint32_t type; };
  #ifndef BOOST_NO_INT64_T
    template <> struct uint_exact<64> { typedef uint64_t type; };
  #endif

And possibly similar structs providing int_least<Bits>::type,
uint_least<Bits>::type, int_fast<Bits>::type, and uint_fast<Bits>::type,
retrieving their types directly from <boost/cstdint.hpp>.

Those structs would be very helpful to me already, and they could serve as
building blocks for whatever clever meta-functions. But what are the
chances of getting those structs into the Boost Integer Library? I'm not a
Boost developer myself. So I guess all I could do is just drop another
ticket at http://svn.boost.org/trac/boost Or would it be more effective to
write some kind of proposal?

> I'm wondering about a special thing like this:
> template <size_t N, typename U = void>
> struct stdint_t { typedef U type; }
> template <typename U = void> struct stdint_t<32,U> {
> typedef int32_t type; } // and etc

Not completely clear to me...

> Suppose that we just went and used the cstdint.hpp method. Would we
> then just using uint_t<N> = uint_exact_t< ((N-1)|7)+1 >;
> Since all the types we know about would be multiples of 8 bits?

Hmmm... I don't really understand this formula either: (((N-1)|7)+1)

>> int_exact<sizeof(T)*8>
>>
> BTW: CHAR_BIT or integer_traits<unsigned char>::digits, not 8 :P

Oops! You're absolutely right! I was very very bad ;-)

> /usr/include/boost/xpressive/traits/cpp_regex_traits.hpp has
> "define an unsigned integral typedef of the same size as
> std::ctype_base::mask"
> So this does seem like a good suggestion.
>
> Easy to implement, too:
> template <typename T>
> struct unsigned_ {
> BOOST_STATIC_ASSERT(integer_traits<T>::is_specialized);
> // and maybe BOOST_STATIC_ASSERT(integer_traits<T>::is_signed);
> typedef
> uint_exact_t<integer_traits<T>::digits+integer_traits<T>::is_signed>::type
> type;
> };
> template <typename T>
> unsigned_<T> make_unsigned(T x) { return unsigned_<T>(x); }

I'm afraid I will lose the value of my argument (x), when passing it to
make_unsigned, because it returns a struct that doesn't have any data! But
at least it will get me the right unsigned type :-)

> Or maybe a new metafunction,
> int_size_as<T> = int_exact_t<sizeof(T)*CHAR_BIT>;

That's possible...

BTW, I think we might drop the "_t", and call the struct "int_exact",
instead of "int_exact_t", when the typedef is simply called "type". Because
IMO int_exact<32>::type is clear enough. :-)

So... any suggestions on how to get those simple structs like int_exact and
uint_exact into the Boost Integer Library?

Kind regards,

  Niels


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net