Chris ( Val ) wrote in message...
[snip and agree]
for( int i(0); size_t( i ) < somevector.size(); ++i ){
// some op that needs an int.
}
... the 'size_t( i )' shuts-up the compiler warning.
Hi Bob,
I am interested to know why you use this version:
for( int i(0); size_t( i ) < somevector.size(); ++i )
...as opposed to this one:
for( size_t i(0); i < somevector.size(); ++i )
?
Isn't your version casting an int to a size_t every time
it the expression is evaluated?
Hi Chris,
Pretty much what Victor said.
My current project would require me to cast hundreds of places if I used the
'for( size_t ...)' version. So, I'd rather cast in one single place, until
I'm ready to tackle the final 23 functions [converting an old 'C' set of
programs into one 'C++' (monster!) program. Creates a 3D object out of a few
random numbers.].
Going from 'int' to 'size_t' is pretty safe (unless a really big number will
cause failure.<G>).
Use the proper type where you can, pick the lesser of two evils in other
places.
When I have a choice, I use the 'size_t', especially when dealing with an
index.
My project did use an negative index, and it took me forever to 'clean'.
struct Thing{ // the 'C' stuff I 'inherited'.
double peak;
double array[6];
};
Thing thing;
for( int i( -1 ); i < 6; ++i ){ // size_t( i) == problem here
thing.array[ i ] = someassign;
// first loop modifies 'peak'!! I HATE that.
}
Drove me nuts for a while! (made 'array' a std::vector, and kept
segfaulting.).
( yeah, I'm still nuts, but not for that construct! <G>).
My project works smooth as glass now, so I'm working on the interfaces
(separating wxWidgets from the 'workhorse', then I'll optimise the
bottlenecks (and should be able to eliminate (most of) the for loops).)
Ooops, running off-topic again. Sorry.
Where as (please correct me if I'm wrong gang), I don't
think the latter version does?
Not AFAIK.
Depends on the requirements/restrictions in the loop body.
[ How do you know the compiler does not optimise it? ]