i dont know where and how to use static_cast<int>(   )
plz help me,
best regards..

Well you really should not need to use static_cast<int>() for built in type conversions and promotions. This is because C++ allows arithmetic types to be converted from one to another or (slightly differently) one to be promoted to another.

Casting of values to other types should be avoided if at all possible. Although you do need casts occasionally, such code should be minimised and the use of a cast indicates something a bit dodgy is going on - so readers of such code should take care pay attention! You will note that the C++ casts are cumbersome and easy to find in code, this is by intent!

However many compilers like to issue warnings when data might be lost - such as when assigning an int to a char or a floating point value to an integer value, or the result may not what you might expect - as when mixing signed and unsigned values.

If you know such warnings are for operations that are both safe and necessary then you can usually suppress such warnings by casting the value to the required type. Take the following simple example:

     double fpValue(123.0);
     int intValue;
     intValue = fpValue * fpValue;
The above code will most likely produce a warning along the lines that the assignment (the =) in the third line requires a conversion from double to int with a possible loss of data (fractional parts of the floating point value will be lost, plus the number may be too big or small to fit meaningfully into an int).
If we know the conversion is safe and/or exactly what we wanted then we should be able to suppress the warning using a static_cast to explicitly cast the right hand side value to an int:

     double fpValue(123.0);
     int intValue;
     intValue = fpValue * fpValue;
     intValue = static_cast<int>(fpValue * fpValue);

Note that I have said both safe and necessary. If you were not aware that you had lost data or the results may not be as expected then you should look very carefully at the code to ensure you are doing what you intended, and if such lossy or dangerous conversions are not necessary, change the code to use types that either require no conversions or use lossless conversions instead - such as using a larger integer type or changing mixed signed/unsigned arithmetic to be all signed or all unsigned.
If they are necessary but could cause bad results for some values then ensure you fix the code to handle these cases if it does not do so already.
For example one common place I get warnings about mixed sign/unsigned comparisons is when using a for-loop. By reflex (i.e without really thinking) I use an int for the loop variable then the compiler points out I am checking this int against some unsigned integer type in the loop termination condition. Often this is when using values from elsewhere, such as sizes of things that return a size_t or other unsigned integer (after all what would the meaning of a negative size be?). In such cases you should really heed the warning and change the type of the loop variable to match the type of the loop termination value.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.