impossible w/o casting

Started by
4 comments, last by asdqwe 16 years, 2 months ago
I've heard casts have to be avoided as much as possible, and can always be avoided. But SOME functions seem to require them. EG:
void main()
{
		int nr;
		nr = strlen("Babe Ruth");
		cout<< nr;
}
That way, I get: warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data. But when I change the offender into:
size_t nr;
I get: warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data. So I'm asking: Can anyone, including Bill Gates, fix this so it doesn't spit warnings OR require typecasts?
Advertisement
Use C++ (the C++ STL in particular):
void main(){		size_t nr;		nr = std::string("Babe Ruth").size();		std::cout << nr;}
Quote:Original post by asdqwe
But when I change the offender into:
size_t nr;
I get: warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data.
So I'm asking: Can anyone, including Bill Gates, fix this so it doesn't spit warnings OR require typecasts?

My compiler doesn’t give any error or warning using size_t in your code.
To nife87: your code still is buggy. Now I get a warning on the std::cout line: warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data
So it's no good!
It sounds like you have the /Wp64 flag enabled for your project. Using this flag gives a lot of spurious warnings. Go to your project settings and disable "Detect 64-bit Portability Issues" in the C++/General section.
Thanks a lot SiCrane!

This topic is closed to new replies.

Advertisement