Avoiding loss of precision in casting

Hey Everyone,

I am currently learning C++, and I had this question about casting.

Example
 
char c = 1000;


in the example above, 1000 will be implicitly cast to a char, and of course there will be loss of precision. My question is, is there a casting function or something in the stl that would throw an exception whenever loss of precision will occur?. The only way I can think of now is to just check the value I am assigning to the char, and check if its within the ascii range of a char. In practice I don't think I'll ever need something like that, but I was just curious if it exists (maybe it will be helpful in some situations?). Btw I am currently using visual C++ 2010, and I also use gcc on linux (saying that just in case this info is needed)

Thanks.
You'll get a compile time warning on a narrowing conversion.

You really shouldn't have any warnings in your final code.
Last edited on
Topic archived. No new replies allowed.