Largest value of an unsigned integral type

D

Dave

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
 
J

Jack Klein

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
 
A

Andrey Tarasevich

Dave said:
...
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 said:
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')
 
J

Jakob Bieling

Dave said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top