what's the 'right' way to get rid of size_t to int conversion warnings?

G

glen stark

Surely there is something clever to do here, besides turning off the
warning?

Glen
 
B

Ben Pope

glen said:
Surely there is something clever to do here, besides turning off the
warning?

Use size_t instead of int.

Or perhaps:

size_t size = 5;
int i = static_cast<int>(size);

Ben Pope
 
B

Bob Hairgrove

Surely there is something clever to do here, besides turning off the
warning?

Yes. Instead of writing this:
vector<something> v;
// fill the vector here...
for (int i=0; i<v.size(); ++i) /* ... */

write this:
for (size_t i=0; i<v.size(); ++i) /* ... */

Even better, use iterators.
 
B

BobR

Bob Hairgrove wrote in message ...
Yes. Instead of writing this:
vector<something> v;
// fill the vector here...
for (int i=0; i<v.size(); ++i) /* ... */

write this:
for (size_t i=0; i<v.size(); ++i) /* ... */

Even better, use iterators.
Bob Hairgrove

OP: Another 'clunky' way to do it:

std::string String("Good");

// for( int i(0); i < String.size(); ++i ){
// [Warning] comparison between signed and unsigned integer expressions

for( int i(0); size_t( i ) < String.size(); ++i ){
std::cout<<"String.at(" << i << ")="<<String.at( i )<<std::endl;
}
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top