C + + __declspec keyword verbose usage
__declspec is used to specify the Microsoft-related storage method for instances of the given type. Other modifiers about storage such as static and extern are ANSI specifications for the C and C + + languages, and __declspec is the definition of an extended property. Extended attribute syntax simplifies and standardizes the C and C + + language extensions for Microsoft.
Usage: __declspec (extended-decl-modifier)
The extended-decl-modifier parameters are as follows, and can appear at the same time, separated by spaces:
Align (c + +)
Allocate
AppDomain
Deprecated (c + +)
DllImport
Dllexport
Jitintrinsic
Naked (c + +)
Noalias
Noinline
Noreturn
Nothrow (c + +)
Novtable
Process
Property (c + +)
Restrict
Selectany
Thread
UUID (c + +)
The 1.__DECLSPEC keyword should appear in front of the simple declaration. The compiler ignores and does not give a warning to __declspec that appear in front of the identifier in the * or & or in the variable declaration.
2. Be careful to distinguish whether __declspec is a cosmetic or cosmetic variable:
__declspec (align (8)) struct STR b; the modifier is variable B. Other locally defined variables of the struct STR type will not be affected by __declspec (align (8)).
__declspec (align (8)) struct STR {}; the struct str type is decorated. All variables of this type are affected by __declspec (align (8)).
Align
Format: __declspec (align (n)) declarator
where n is the alignment parameter, its valid value is the whole number of powers (from to bytes), such as,,,, or. The parameter declarator is the data to set the alignment.
1. Use __declspec (align (n)) to precisely control the alignment of user-defined data. You can use __declspec (align (n)) when defining struct,union,class or declaring variables.
2. __declspec (align (n)) cannot be used for function parameters.
3. If __declspec (#) is not used, the compiler will align according to the data size according to the natural boundary. such as byte integers are aligned by byte boundaries, and byte pairs are aligned by byte bounds. The data in a class or struct, aligns the natural alignment of the data itself with the minimum value in the alignment factor set by the #pragma pack (n).
4.__declspec (Align (n)) and #pragma pack (n) are a pair of siblings that specify the minimum value of the alignment factor, which specifies the maximum value of the alignment factor.
5. When both are present, the former has a higher priority. That is, when both appear and the values contradict, the latter will not work.
6. This __declspec (align (n)) designation will not work when the variable size is greater than or equal to n specified by #pragma pack (n), and __declspec (align (n)) specifies a value of n that is smaller than the length of the corresponding type.
7. This value n will not work when #pragma pack (n) specifies a value of n greater than or equal to all data member size.
Allocate
Format: __declspec (allocate ("Segname")) declarator
Specifies the stored data segment for the data. The data segment name must be one of the following:
Code_seg
Const_seg
Data_seg
Init_seg
Section
AppDomain
Specifies that each application domain in the managed program should have a copy of the specified global variable or static member variable.
Deprecated
The same effect as #pragma deprecated (). One of the overloaded forms used to specify a function is not recommended. When a function that is deprecated decorated is called in the program, the compiler gives a C4996 warning and can specify a specific warning message. This warning message can be derived from a defined macro.
For example:
Compile with:/W3
#define MY_TEXT "function is deprecated"
void Func1 (void) {}
__declspec (deprecated) void func1 (int) {}
__declspec (Deprecated ("* * This is a deprecated function * *")) void Func2 (int) {}
__declspec (deprecated (my_text)) void func3 (int) {}
int main () {
Func1 ();
FUNC1 (1); C4996, warning message: Warning C4996: ' func1 ': was declared deprecated
FUNC2 (1); C4996, warning message: Warning C4996: ' FUNC2 ': * * This is a deprecated function * *
FUNC3 (1); C4996, warning message: Warning C4996: ' func3 ': function is deprecated
}
Dllimport,dllexport:
Format:
__declspec (dllimport) declarator
__declspec (dllexport) declarator
Each is used to import functions, data, or objects from a DLL, as well as to export functions, data, or objects from a DLL. Equivalent to the interface that defines the DLL, defines the functions, data, or objects that can be used for its client EXE or DLL.
Declaring a function as dllexport eliminates the definition of the module definition (. DEF) file.
Dllexport replaced the __export keyword.
The function names that are exported by C + + functions declared as dllexport are processed according to C + + rules. If you require name processing that does not follow C + + rules, use a. def file or use extern "C".
Jitintrinsic:
Format: __declspec (jitintrinsic)
Used to mark a function or element as a bit common language runtime (CLR). Used primarily in some libraries provided by Microsoft.
Using Jitintrinsic will add modopt (isjitintrinsic) to the function signature.
Naked
Format: __declspec (naked) declarator
This keyword is used only for x86 systems and is used more for virtual device drivers. This keyword allows the compiler to generate code without any comments or tags. You can use only the definition of a function, not for data declarations, definitions, or declarations of functions.
Noalias:
Applies only to functions, which indicate that the function is a semi-pure function. A semi-pure function refers to referencing or modifying only local variables, parameters, and first-level indirect parameters. It is a promise to the compiler that if the function references a global variable or a second-level indirect pointer parameter, the compiler generates code that interrupts the application.
Restrict
Format: __declspec (restrict) return_type f ();
Applies only to function declarations or definitions that return pointers, such as the malloc function of the CRT: __declspec (restrict) void *malloc (size_t size), which tells the compiler that the pointer returned by the function is not confused with any other pointers. It provides the compiler with more information to perform compiler optimizations. One of the biggest difficulties for compilers is determining which pointers are confused with other pointers, and using this information is useful for compilers. It is important to note that this is a promise to the compiler that the compiler does not validate it. If your program uses __declspec (restrict) improperly, the behavior of the program is incorrect.
Noinline:
Because member functions defined in the class definition are inline by default, __declspec (naked) is used to explicitly specify that a function in a class does not require inline (inline). If a function is small and has little effect on system performance, it is necessary to declare it as non-introverted. For example, a function to handle an error condition.
Noreturn:
A function is modified by __declspec (noreturn), then its meaning is to tell the compiler that the function does not return, and the result is that the compiler knows that the code after the function that is decorated with __declspec (noreturn) is unreachable.
If the compiler discovers that a function has a code branch with no return value, the compiler will report a C4715 warning or C2202 an error message. If the code branch is not reachable because the function does not return, you can use the contract __declspec (noreturn) to avoid the above warning or error.
The Convention of a function that expects to be returned as __declspec (noreturn) results in undefined behavior.
In the following example, the main function is not returned from the Else branch, so the contract function fatal to __declspec (noreturn) to avoid compiling or warning messages.
__declspec (noreturn) extern void Fatal () {}
int main () {
if (1)
return 1;
else if (0)
return 0;
Else
Fatal ();
}
Nothrow:
Format: Return-type __declspec (nothrow) [call-convention] Function-name ([argument-list])
can be used for function declarations. Tells the compiler that the function being declared and other functions that are called inside the function will not throw an exception.
Novtable:
Can be used in any class declaration, but preferably only for pure interface classes, where the class itself is never instantiated. The declaration of this keyword will prevent the compiler from initializing the vfptr of the constructor and destructor. You can optimize the post-compilation code size.
If you attempt to instantiate a class that is declared with __declspec (novtable) and then access members in the class, an access error (Access violation, or AV) is generated at run time.
Process
Indicates that your managed application process should have a copy of the specified global variables, static member variables, or static local variables shared by all application domains. When compiling with/clr:pure, you should use __declspec (process) because, by default, each application domain has a copy of the global and static variables when compiling with/clr:pure. When compiling with/clr, you do not have to use __declspec (process) because, by default, each process has a copy of the global and static variables when compiling with/clr.
Only global variables, static member variables, or local static variables of local types can be decorated with __declspec (process).
When compiling with/clr:pure, variables declared as __declspec (process) should also be declared as const types.
Use the AppDomain when you want to have a copy of the global variables for each application domain.
Property
Format:
__declspec (get=get_func_name) declarator
__declspec (put=put_func_name) declarator
__declspec (get=get_func_name, put=put_func_name) declarator
This property can be used for non-static "virtual data members" in a class or struct definition. It's actually a mapping that maps your methods to properties for access. Get and put are permissions for property access, one for read permissions, and one for write permissions. When the compiler sees a property-decorated data member appearing to the right of the member selector ("." or "-a"), it will convert the operation into a get or put method. The modifier can also be used in an empty array in a class or struct definition.
Use the following:
struct S {
int i;
void Putprop (int j) {
i = j;
}
int Getprop () {
return i;
}
__declspec (get = getprop, put = putprop)) int the_prop;
};
int main () {
s S;
S.the_prop = 5;
return s.the_prop;
}
Selectany
Format: __declspec (selectany) declarator
The Mfc,atl source is flooded with __declspec (selectany) declarations. Selectany can let us initialize a global variable in the. h file instead of only in. cpp. For example, there is a class, which has a static variable, then we can initialize this global variable in. h by a similar __declspec (selectany) type class::variable = value; The. h is multiple include, and the linker also rejects multiple definitions of errors for us. There is a lot of convenience for template programming.
Use the following:
__declspec (selectany) int x1=1; Correct, X1 is initialized, and externally visible
Const __declspec (selectany) int x2 = 2; Error, in C + +, const is static by default, but is correct in C, and Const is not static by default
extern const __declspec (selectany) int x3=3; Correctly, the X3 is extern const, visible to the outside
extern const int x4;
Const __declspec (selectany) int x4=4; Correctly, the X4 is extern const, visible to the outside
extern __declspec (selectany) int x5; Error, X5 not initialized, cannot be modified with __declspec (selectany)
Class X {
Public
X (int i) {i++;};
int i;
};
__declspec (selectany) x x (1); Correct, dynamic initialization of global objects
Thread
Format: __declspec (thread) declarator
Declares that declarator is a thread-local variable and has a thread-store time limit so that the linker can schedule the storage that is automatically allocated when the thread is created.
Thread-local Storage (TLS) is a mechanism in which each thread allocates its own local data in a multithreaded runtime environment. In a standard multithreaded program, data is shared among multiple threads, and TLS is a mechanism for assigning its own local data to each thread.
This property can only be used for declarations and definitions of data or classes that do not have member functions, and cannot be used for function declarations and definitions.
The use of this property may affect delayed loading of DLLs.
This property can be used only for static data, including global data Objects (static and extern), local static objects, static data members of classes, and not for automatic data objects.
The attribute must be used for both the declaration and definition of the data, regardless of whether it is declared and defined in one file or multiple files.
__declspec (thread) cannot be used as a type modifier.
If the object is not defined at the same time as the class declaration, __declspec (thread) is ignored, for example:
Compile with:/ld
__declspec (thread) class X
{
Public
int I;
} x; X is a thread object
X y; Y is not a thread object
The following two examples are semantically identical:
__declspec (thread) class B {
Public
int data;
} bobject; Bobject is a thread object
Class B2 {
Public
int data;
};
__declspec (thread) B2 BObject2; BObject2 is a thread object
Uuid:
Format: __declspec (uuid ("Comobjectguid")) declarator
Declare registered content with a unique identifier as a variable that can be called using __uuidof ().
Use the following:
struct __declspec (uuid ("00000000-0000-0000-c000-000000000046")) IUnknown;
struct __declspec (uuid ("{00020400-0000-0000-c000-000000000046}")) IDispatch;
From:
http://blog.csdn.net/iamoyjj/article/details/4195635
More detailed Documentation: HTTP://MSDN.MICROSOFT.COM/EN-US/LIBRARY/DABB5Z75 (v=vs.80). aspx
C + + __declspec keyword verbose usage