P: n/a

Hello all,
Suppose you have an unsigned integral type T. It's not one of the builtin
types, but rather typedefed off of one of the builtin 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 standardconforming compiler?
I'm not using numeric_limits<> to get the maximum value because, as stated
above, I don't know the underlying builtin 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  
Share this Question
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 builtin types, but rather typedefed off of one of the builtin 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 standardconforming 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://JKTechnology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/Cfaq/top.html
comp.lang.c++ http://www.parashift.com/c++faqlite/
alt.comp.lang.learn.cc++ ftp://snursel.org/pub/acllcc++/faq  
P: n/a

Dave wrote: ... Suppose you have an unsigned integral type T. It's not one of the builtin types, but rather typedefed off of one of the builtin 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 standardconforming 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 builtin 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
builtin type". From the above 'static_cast' example it follows that you
know the typedefname 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  
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
builtin types, but rather typedefed off of one of the builtin 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 standardconforming compiler?
I'm not using numeric_limits<> to get the maximum value because, as stated above, I don't know the underlying builtin 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 email)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 5728
 replies: 3
 date asked: Jul 19 '05
