Using pragmas

Pragmas are compiler-specific and often are concerned with the technical details about the code sections in the object files. There are a couple of Visual C++ pragmas that are useful in debugging code.

In general, you will want your code to compile with as few warnings as possible. The default warning for the Visual C++ compiler is /W1, which means that only the most severe warnings are listed. Increasing the value to 2, 3, or the highest value of 4 progressively increases the number of warnings that are given during a compilation. Using /Wall will give level-4 warnings and warnings that have been disabled by default. This last option, even for the simplest code, will produce a screen full of warnings. When you have hundreds of warnings useful error messages will be hidden between the reams of unimportant warnings. Since the C++ Standard Library is complex and uses some code that is decades old, there are some constructs that the compiler will warn you about. To prevent these warnings polluting the output from your builds, specific warnings in selective files have been disabled.

If you are supporting older library code, you may find that the code compiles with warnings. You may be tempted to reduce the warning levels using the compiler /W switch, but that will suppress all warnings higher than the ones you enable, and it applies equally to your code as to the library code that you may be including into your project. The warning pragma gives you a lot more flexibility. There are two ways to call this--you can reset the warning level to override the compiler /W switch and you can change the warning level of a particular warning or disable the warning reporting altogether.

For example, at the top of the <iostream> header is the line:

    #pragma warning(push,3)

This says store the current warning level and, for the rest of this file (or until it is changed), make the warning level 3. At the bottom of the file is the line:

    #pragma warning(pop)

This restores the warning level to that stored earlier.

You can also change how one or more warnings are reported. For example, at the top of <istream> is:

    #pragma warning(disable: 4189)

The first part of this pragma is the specifier disable, which indicates that reporting of a warning type (in this case, 4189) is disabled. If you choose, you can change the warning level of a warning by using the warning level (1, 2, 3, or 4) as the specifier. One use for this is to lower the warning level just for a piece of code that you are working on and then return it to its default level after the code. For example:

    #pragma warning(2: 4333) 
unsigned shift8(unsigned char c)
{
return c >> 8;
}
#pragma warning(default: 4333)

This function shifts a char right by 8 bits, which will generate the level-1 warning 4333 (right shift by too large amount, data loss). This is a problem and needs to be fixed, but for the time being, you want to compile the code without warnings from this code and so the warning level is changed to level 2. Using the default warning level (/W1) the warning will not be shown. However, if you compile with a more sensitive warning level (for example, /W2) then this warning will be reported. This change in the warning level is only temporary because the last line resets the warning level back to its default (which is 1). In this case, the warning level is increased, meaning that you will only see it with a more sensitive warning level on the compiler. You can also reduce the warning level, which means that the warning is more likely to be reported. You can even change a warning level to error so the code will not compile while warnings of this type exist in the code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset