Largest value of an unsigned integral type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    Largest value of an unsigned integral type


    Hello all,

    Suppose you have an unsigned integral type T. It's not one of the built-in
    types, but rather typedefed off of one of the built-in unsigned integral
    types (but we don't know which one).

    I want to find the maximum value for this type. This seems to work just
    fine:

    static_cast<T>(-1)

    Is there any reason this should be avoided? Or is this indeed guaranteed to
    do the job on a standard-conforming compiler?

    I'm not using numeric_limits< > to get the maximum value because, as stated
    above, I don't know the underlying built-in type, so I don't know which
    specialization of numeric_limits< > to use.

    In case anyone is wondering, the type I'm actually working with is
    string::size_ty pe. I need to get the maximum value for this type, but,
    according to Josuttis, I can only be assured the underlying type is unsigned
    integral, but there are no guarantees as to which of the unsigned integral
    types it is...

    Does this look clean to everyone?

    Thanks,
    Dave


  • Jack Klein

    #2
    Re: Largest value of an unsigned integral type

    On Thu, 6 Nov 2003 20:53:30 -0700, "Dave" <better_cs_now@ yahoo.com>
    wrote in comp.lang.c++:
    [color=blue]
    >
    > Hello all,
    >
    > Suppose you have an unsigned integral type T. It's not one of the built-in
    > types, but rather typedefed off of one of the built-in unsigned integral
    > types (but we don't know which one).
    >
    > I want to find the maximum value for this type. This seems to work just
    > fine:
    >
    > static_cast<T>(-1)
    >
    > Is there any reason this should be avoided? Or is this indeed guaranteed to
    > do the job on a standard-conforming compiler?[/color]

    This is guaranteed to work. The conversion of any a value of any
    integer type to any unsigned integer type is well defined. If the
    value being assigned is outside the range of the destination unsigned
    type, it is adjusted by repeatedly adding or subtracting (UTYPE_MAX +
    1) until the value is within the range [0...UTYPE_MAX], so converting
    -1 to any unsigned yields the maximum value.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

    Comment

    • Andrey Tarasevich

      #3
      Re: Largest value of an unsigned integral type

      Dave wrote:[color=blue]
      > ...
      > Suppose you have an unsigned integral type T. It's not one of the built-in
      > types, but rather typedefed off of one of the built-in unsigned integral
      > types (but we don't know which one).
      >
      > I want to find the maximum value for this type. This seems to work just
      > fine:
      >
      > static_cast<T>(-1)
      >
      > Is there any reason this should be avoided? Or is this indeed guaranteed to
      > do the job on a standard-conforming compiler?[/color]

      Yes, it is guaranteed to do the job.
      [color=blue]
      > I'm not using numeric_limits< > to get the maximum value because, as stated
      > above, I don't know the underlying built-in type, so I don't know which
      > specialization of numeric_limits< > to use.[/color]

      I don't understand why do you care about knowing "the underlying
      built-in type". From the above 'static_cast' example it follows that you
      know the typedef-name of the type, don't you? You can immediately use it
      with 'std::numeric_l imits'

      std::numeric_li mits<T>::max();
      [color=blue]
      > In case anyone is wondering, the type I'm actually working with is
      > string::size_ty pe. I need to get the maximum value for this type, but,
      > according to Josuttis, I can only be assured the underlying type is unsigned
      > integral, but there are no guarantees as to which of the unsigned integral
      > types it is...[/color]

      You can simply use 'std::numeric_l imits<std::stri ng::size_type>: :max()'.

      Actually, 'std::string::n pos' is guaranteed to represent the largest
      value of 'std::string::s ize_type' (it is initialized by using the same
      trick with '-1')

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Jakob Bieling

        #4
        Re: Largest value of an unsigned integral type

        "Dave" <better_cs_now@ yahoo.com> wrote in message
        news:vqm5u9jsln i20d@news.super news.com...[color=blue]
        >
        > Hello all,
        >
        > Suppose you have an unsigned integral type T. It's not one of the[/color]
        built-in[color=blue]
        > types, but rather typedefed off of one of the built-in unsigned integral
        > types (but we don't know which one).
        >
        > I want to find the maximum value for this type. This seems to work just
        > fine:
        >
        > static_cast<T>(-1)
        >
        > Is there any reason this should be avoided? Or is this indeed guaranteed[/color]
        to[color=blue]
        > do the job on a standard-conforming compiler?
        >
        > I'm not using numeric_limits< > to get the maximum value because, as stated
        > above, I don't know the underlying built-in type, so I don't know which
        > specialization of numeric_limits< > to use.[/color]

        Sure you do. numeric_limits <T> works. If this would not work, then the
        static_cast would not work either, there, too, you have to know T at compile
        time. Think of it as a template function. Maybe it is clearer then.
        [color=blue]
        > In case anyone is wondering, the type I'm actually working with is
        > string::size_ty pe. I need to get the maximum value for this type, but,
        > according to Josuttis, I can only be assured the underlying type is[/color]
        unsigned[color=blue]
        > integral, but there are no guarantees as to which of the unsigned integral
        > types it is...
        >
        > Does this look clean to everyone?[/color]

        hth
        --
        jb

        (replace y with x if you want to reply by e-mail)


        Comment

        Working...