S
Song Yun Zhao
Hi,
Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?
Personally I don't use uint that much, but I like to optimize my code
and make it as effective as possible. So I feel that using an int where
only an uint is needed is a waste.
e.g. something like (int i = 0; i < 100; i++)
could probably better be written as (uint i = 0; i < 100; i++)
or maybe even better (unsigned short i = 0; i < 100; i++)
One big advantage of using uint is that if I have a function like
void foobar(uint i)
{
if (i >= 0) {
cout << "generate a warning, this is always true" << endl;
}
}
and calling foobar(-1) will generate a warning. This can help me iron
out some problems/bugs at compile time.
However using uint does require more work, for example when comparing an
uint to int will generate a warning, hence would require a static_cast.
Anyway, I want to know what other programmers are doing. I use Qt a lot
at work, and I don't see a lot of uint in Qt's code. Could this be for
portability reasons?
Anyway all comments are appricated. TIA.
Song
Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?
Personally I don't use uint that much, but I like to optimize my code
and make it as effective as possible. So I feel that using an int where
only an uint is needed is a waste.
e.g. something like (int i = 0; i < 100; i++)
could probably better be written as (uint i = 0; i < 100; i++)
or maybe even better (unsigned short i = 0; i < 100; i++)
One big advantage of using uint is that if I have a function like
void foobar(uint i)
{
if (i >= 0) {
cout << "generate a warning, this is always true" << endl;
}
}
and calling foobar(-1) will generate a warning. This can help me iron
out some problems/bugs at compile time.
However using uint does require more work, for example when comparing an
uint to int will generate a warning, hence would require a static_cast.
Anyway, I want to know what other programmers are doing. I use Qt a lot
at work, and I don't see a lot of uint in Qt's code. Could this be for
portability reasons?
Anyway all comments are appricated. TIA.
Song