To do a good job, you must first sharpen the tool. To use VC to develop efficient programs, you must have a full understanding of the VC compiler.
1:/GR (enable runtime type information)
Note: This option (/GR) adds code to check the object type at runtime. When this option is specified, the compiler defines _CpprttiPreprocessor macro. By default, this option is cleared (/GR -).
Set this compiler option in the Visual Studio development environment
- Open the properties page dialog box for this project.
- Click the "C/C ++" folder.
- Click the language attribute page.
- Modify the "enable runtime type information" attribute.
Example: We use typeid to obtain the type of the variable at runtime.
// Expre_typeid_operator.cpp
// Compile with:/GR/ESCs
# Include <iostream>
# Include <typeinfo. h>
Class base {
Public:
Virtual void vvfunc (){}
};
Class derived: public base {};
Using namespace STD;
Int main ()
{
Derived * Pd = new derived;
Base * pb = Pd;
Cout <typeid (PB). Name () <Endl; // prints "class base *"
Cout <typeid (* pb). Name () <Endl; // prints "class derived"
Cout <typeid (PD). Name () <Endl; // prints "class derived *"
Cout <typeid (* PD). Name () <Endl; // prints "class derived"
Delete PD;
}
2:/EH (Exception Handling Model)
/EH {S | A} [C] [-]
This option specifies the exception handling model used by the compiler.
- Use/EHS to specify the synchronization Exception Handling Model (no C ++ Exception Handling for structured exception handling exceptions ). If you use/EHS, do not rely on the compiler to catch asynchronous exceptions.
- Use/EHA to specify the asynchronous Exception Handling Model (C ++ exception handling with structured exception handling exceptions ).
/EHC options require/EHS,/EHA, or/GX. It notifies the compiler to assumeExternThe C function never raises an exception.
This option can be cleared by symbol. For example,/ESCs-are interpreted as/EHS/EHC-and are equivalent to/EHS.
Set this compiler option in the Visual Studio development environment
- Open the properties page dialog box for this project.
- Click the "C/C ++" folder.
- Click the "Code Generation" property page.
- Modify the "enable C ++ exception" attribute.
Or
- Click the "C/C ++" folder.
- Click the "Code Generation" property page.
- Set "enable C ++ exception" to "no ".
- Click the "command line" attribute page.
- In the additional options box, type compiler options.
/ZC (consistency)
The/ZC compiler option enables you to specify standard behavior with/Ze. The following is a list of/ZC compiler options:
/ZC: forscope
/ZC: wchar_t
(1):/ZC: forscope (enforce consistency in the for loop range)
The standard act of MS is to enableForThe initial values of the loop are
ForBeyond the range after the loop. Under/Ze,ForThe initial value of the loop is kept within the range until the local range ends.
You can use conform to modify the runtime behavior of/ZC: forscope.
If/ZC: forscope is used in a project with an existing. PCH file, ignore/ZC: forscope (with a warning) and continue to compile with the existing. PCH file. To generate a new. PCH file, use/YC.
Set this compiler option in the Visual Studio development environment
- Open the properties page dialog box for this project.
- Click the "C/C ++" folder.
- Click the language attribute page.
- Modify the "consistency in the scope of the mandatory for loop" attribute.
Example:
The following code will be compiled under/Ze, but not under/Za:
int main() {
// int i;
{
For (INT I = 0; I <1; I ++) // In/Ze: forscope compiling mode, assign values below
; // I = 20 will prompt that I cannot be defined. If the compilation mode is set to/za, this issue will not occur.
i = 20;
}
}
When using/ZC: forscope, if the variable is in a range because of a declaration made in the previous range, a warning is given. To illustrate this, remove
//
Characters to declareint i
.
(2)/ZC: wchar_t (wchar_t is of the local type)
If/ZC: wchar_t is not specified, the compiler requires you or definesWchar_t, Or include definition
Wchar_tOne of the many header files (such as wchar. h ).Wchar_tUsually defined
Unsigned short.
Type when/ZC: wchar_t compiler option is specifiedWchar_tIng
_ Wchar_tThe ing methodShortMap_ Int16Same.
If/ZC: wchar_t is used, the compiler willWchar_tRecognized as a local type. Related
Wchar_tFor more information, see data type range.
_ Wchar_tAlways available.
You can create a library that can easily be linked by using (or not using)/ZC: wchar_t compiled code by using the unsigned short format of wchar_t and the _ wchar_t variant, and there is no need to provide two different versions of the Library (Enabled or not enabled/ZC: wchar_t version ).
When/ZC: wchar_t is specified_ Wchar_t_definedAnd_ Native_wchar_t_definedSymbol. For more information, see predefined macros.
Set this compiler option in the Visual Studio development environment
- Open the properties page dialog box for this project.
- Click the "C/C ++" folder.
- Click the language attribute page.
- Modify the "treat wchar_t as a built-in type" attribute.
Example:
None
/Ob (inline function expansion)
/Ob {0 | 1 | 2}
"Inline function expansion" (/obN) Option controls the Inline expansion of the function, whereNIs one of the following:
Option |
Description |
/Ob0 |
Disable Inline expansion (enabled by default ). |
/Ob1 |
Expand only the labels as inline or_ InlineOr the C ++ member functions defined in the class declaration. |
/Ob2 |
ExpandInlineOr_ InlineFunction and any other function selected by the compiler (expanded by the compiler itself, usually called automatic inline ). This option requires that optimization be enabled with/O1,/O2,/Ox, or/og. |
The compiler considers Inline expansion options and Keywords as suggestions. It is not guaranteed that the function will be expanded inline. Cannot force the compiler to inline a specific function.
You can also use # pragma auto_inline to exclude some functions from being considered as a candidate function for Inline expansion. For more information, see # pragma intrinsic.
Set this compiler option in the Visual Studio development environment
- Open the properties page dialog box for this project.
- Click the "C/C ++" folder.
- Click the "optimize" attribute page.
- Modify the "inline function expansion" attribute.
Example:
1:
Class
{
A () {}; // implicit inline mode
Void fun () {cout <"ob" <Endl;} // implicit inline mode
}
The functions defined in the class or structure are automatically set to the inline mode.
Ms also has a keyword _ forceinline (Force inline). After this keyword is used, if the function is not _ forceinline, a warning is generated (the Warning number is 4714)
If the function or compiler is set to the following, inline ,__ inline ,__ forceinline will be invalid.
1: closed in inline mode (/ob0) (default setting in debug mode)
2: Functions and callers use different exception types.
3: Variable Parameter Functions
4: The function uses the inline keyword, but does not use the compilation settings/og (Global Optimization),/Ox (fully optimized),/O1 (minimized size ), or/O2 (minimum speed ).
5: The function uses the inline keyword. The compilation option does not set the return value to check the exception type. The compilation option is/GX (enable Exception Handling),/EHS, or/EHA (which notifies the compiler to assume
ExternC function never raises an exception)
6: The function accepts a copy constructor object with an exception type, but enables/GX (enable Exception Handling),/EHS, or/EHA (which notifies the compiler to assume
ExternC function never raises an exception)
7: The function is a loop body and is not specified by # pragma inline_recursion (on. (With The Pragma option, the loop body function runs inline and the number of cycles in the loop body is 8 times. You can set it through inline_depth)
8: a function is a virtual function or called by a virtual function. (The virtual function is called directly by inline)
9: The program obtains a function address and uses a pointer to access this address. The function is inlined.
10: The function call convention is naked _ declspec.