By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Largest value of an unsigned integral type

P: n/a

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_type. 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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 6 Nov 2003 20:53:30 -0700, "Dave" <be***********@yahoo.com>
wrote in comp.lang.c++:

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?


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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #2

P: n/a
Dave wrote:
...
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?
Yes, it is guaranteed to do the job.
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.
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_limits'

std::numeric_limits<T>::max();
In case anyone is wondering, the type I'm actually working with is
string::size_type. 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...


You can simply use 'std::numeric_limits<std::string::size_type>::max( )'.

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

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3

P: n/a
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...

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.
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.
In case anyone is wondering, the type I'm actually working with is
string::size_type. 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?


hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.