If I have an int and a size_t variable,can I compare them like:
int i=1;
size_t y=2;
if(i==y)
// do something..
or do I have to type-cast one of them?
If I have an int and a size_t variable,can I compare them like:
int i=1;
size_t y=2;
if(i==y)
// do something..
or do I have to type-cast one of them?
If you're going to compare an int type to size_t(or any other library type), you are doing a risky comparison because int is signed and size_t is unsigned, so one of them will be implicitly converted depending on your compiler/platform. The best thing to do, is to rewrite your int i as:
decltype(y.size()) i = 1;
This assigns your i as the safe type you're trying to compare, and I think it's good practice. This solution is useful in all types of iterators as well. You generally don't want to trust the compiler to cast for you, you can, but it's risky, and an unnecessary risk.