VC _ declspec keyword

Source: Internet
Author: User

Http://hi.baidu.com/henfengduandie/item/f13afbca610c20d696445285VC _ declspec keyword

Align:
Format: __declspec (align (N) declarator
Where, n is the alignment parameter, and its valid value is the integer power of 2 (from 1 to 8192 bytes), such as 2, 4, 8, 16, 32, or 64. The declarator parameter is the data to set 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 declaration variables )).
2. _ declspec (align (N) cannot be used for function parameters )).
3. If _ declspec (align (#) is not used, the compiler will align the data size according to the natural boundary. For example, a 4-byte integer is aligned at the 4-byte boundary, and an 8-byte double is aligned at the 8-byte boundary. The data in the class or struct is aligned with the natural alignment of the data and the minimum value in the alignment coefficient set by # pragma pack (n.
4. _ declspec (align (N) and # pragma pack (n) are brothers. The former specifies the minimum alignment coefficient, and the latter specifies the maximum alignment coefficient.
5. When both of them appear at the same time, the former has a higher priority. That is, when both are present and the values are in conflict, the latter will not work.
6. when the variable size is greater than or equal to the value n specified by # pragma pack (N) and the value n specified by _ declspec (align (n) is smaller than the corresponding type length, this _ declspec (align (N) does not work.
7. When the value n specified by # pragma pack (n) is greater than or equal to the size of all data members, this value n does not work.

Allocate:
Format: __declspec (allocate ("segname") declarator
Specifies the data segment to be stored for the data. The data segment name must be one of the following:
Code_seg
Const_seg
Data_seg
Init_seg
Section

Appdomain:
Each application domain in the specified hosting program must have a copy of the specified global variable or static member variable.

Deprecated:
It works the same as # pragma deprecated. It is not recommended to specify an overloaded form of a function. When a function modified by deprecated is called in a program, the compiler provides a c4996 warning and can specify specific warning information. The warning information can be derived from the 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: Warning c4996: 'function1': was declared deprecated
Func2 (1); // c4996, warning: Warning c4996: 'function2': ** this is a deprecated function **
Func3 (1); // c4996, warning: Warning c4996: 'func3': function is deprecated
}

Dllimport, dllexport:
Format:
_ Declspec (dllimport) declarator
_ Declspec (dllexport) declarator
It is used to import functions, data, or objects from the DLL, and to export functions, data, or objects from the DLL. It is equivalent to defining the DLL interface and defining available functions, data, or objects for its customer EXE or DLL.
If the function is declared as dllexport, you can avoid defining the module definition (. Def) file.
Dllexport replaces the _ export keyword.
When the C ++ function declared as dllexport is exported, the function name will be processed according to the C ++ rule. If you do not need to follow the C ++ rules, use the. Def file or use extern "C ".

Jitintrinsic:
Format: __declspec (jitintrinsic)
It is used to mark a function or element as a 64-bit Universal Language Runtime (CLR ). It is mainly used in some libraries provided by Microsoft.
When jitintrinsic is used, modopt (isjitintrinsic) is added to the function signature ).

Naked:
Format: __declspec (naked) declarator
This keyword is only used for x86 systems and is mostly used for virtual device drivers. This keyword allows the compiler to generate code without any comments or tags. It can only be used for function definitions. It cannot be used for data declaration, definition, or function declaration.

Noalias:
Only applicable to functions. It indicates that the function is a semi-pure function. A semi-pure function refers to only referencing or modifying local variables, parameters, and the first level of indirect parameters. It is a promise to the compiler. 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 ();
Only applies to function declarations or definitions that return pointers. For example, the malloc function of CRT: __declspec (restrict) void * malloc (size_t size ); it tells the compiler that the pointer returned by the function will not be confused with any other pointer. It provides more information for the compiler to execute Compiler optimization. One of the greatest difficulties for the compiler is to determine which pointers will be confused with other pointers, and using this information is very helpful to the compiler. It is worth noting that this is a commitment to the compiler and the compiler does not validate it. If your program uses _ declspec (restrict) improperly, the program behavior is incorrect.

Noinline:
Because the member functions defined in the class definition are all inline by default, __declspec (naked) is used to explicitly specify a function in the class without inline ). If a function is small and has little impact on system performance, it is necessary to declare it unrestrained. For example, a function is used to handle errors.

Noreturn:
If a function is modified by _ declspec (noreturn), it means to tell the compiler that this function will not be returned. The result is to let the compiler know that it is modified to _ declspec (noreturn) the code after the function is not reachable.
If the compiler finds that a function has a code branch with no return value, the compiler reports a c4715 warning or c2202 error message. If the code branch cannot be reached because the function does not return, you can use the Convention _ declspec (noreturn) to avoid the above warning or error.
Conventions of a function to be returned as _ declspec (noreturn) will lead to undefined behavior.
In the following example, the main function is not returned from the else branch, so the fatal of the convention function is _ declspec (noreturn) to avoid compilation or warning information.
_ 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 declaration. It tells the compiler that the declared function and other functions called within the function will not throw an exception.

Novtable:
It can be used in any class declaration, but it is best to use it only for pure interface classes, that is, the class itself is never instantiated. The declaration of this keyword will prevent the compiler from initializing the vfptr of the constructor and destructor. The size of the compiled code can be optimized.
If you try to instantiate a class declared with _ declspec (novtable) and then a member of the lifecycle class, an access violation (AV) will be generated at runtime ).

Process:
Indicates that your managed application process should have a copy of the specified global variable, static member variable, or static local variable shared by all application domains. When using/CLR: pure for compilation, use _ declspec (process) Because/CLR: Pure is used for compilation. By default, each application domain has a copy of global and static variables. When using/CLR for compilation, you do not need to use _ declspec (process) Because/CLR is used for compilation. By default, each process has a copy of global and static variables.
Only global variables, static member variables, or local static variables of the local type can be modified with _ declspec (process.
When using/CLR: pure for compilation, the variable declared as _ declspec (process) should also be declared as the const type.
If you want each application domain to have a copy of global variables, use appdomain.

Property:
Format:
_ Declspec (Property (get = get_func_name) declarator
_ Declspec (Property (put = put_func_name) declarator
_ Declspec (Property (get = get_func_name, put = put_func_name) declarator
This attribute can be used for non-static "virtual data member" in the class or structure definition ". In fact, it is a ing to map your methods into attributes for access. Get and put are the attribute access permissions. One is the read permission and the other is the write permission. When the compiler sees the data member modified by property appear in the member selector (". "or"-> "), it converts the operation to the get or put method. This modifier can also be used as an empty array in a class or structure definition.
The usage is as follows:
Struct s {
Int I;
Void putprop (Int J ){
I = J;
}
Int getprop (){
Return I;
}
_ Declspec (Property (get = getprop, put = putprop) int the_prop;
};

Int main (){
S;
S. the_prop = 5;
Return S. the_prop;
}

Selectany:
Format: __declspec (selectany) declarator
In MFC, the source code of ATL is filled with the declaration of _ declspec (selectany. Selectany allows us to initialize a global variable in the. h file instead of placing it in. cpp. For example, if there is a class with a static variable, we can. h uses code like _ declspec (selectany) type class: Variable = value; to initialize this global variable. This. H is included multiple times, and the linker also removes multiple definition Errors for us. There will be a lot of convenience for template programming.
The usage is as follows:

_ Declspec (selectany) int X1 = 1; // correct. X1 is initialized and visible to the outside

Const _ declspec (selectany) int X2 = 2; // error. In C ++, const is static by default, but is correct in C, by default, the const is not static.

Extern const _ declspec (selectany) int X3 = 3; // correct, X3 is extern const, Which is externally visible

Extern const int X4;
Const _ declspec (selectany) int X4 = 4; // correct. X4 is extern const, Which is externally visible.

Extern _ declspec (selectany) int X5; // error. X5 is not initialized and cannot be modified with _ declspec (selectany)

Class X {
Public:
X (int I) {I ++ ;};
Int I;
};

_ Declspec (selectany) x (1); // correct, dynamic initialization of Global Objects

Thread:
Format: __declspec (thread) declarator
Declare the declarator as a local variable of the thread and have a time limit for storing the thread, so that the linker schedules the storage automatically allocated when creating the thread.
Thread Local Storage (TLS) is a mechanism in which each thread allocates its own local data in a multi-threaded running environment. In standard multi-threaded programs, data is shared among multiple threads, while TLS is a mechanism for allocating local data to each thread.
This attribute can only be used for the declaration and definition of data or classes without member functions. It cannot be used for the declaration and definition of functions.
This attribute may affect DLL loading delay.
This attribute can only be used for static data, including Global Data Objects (static and extern), local static objects, static data members of the class, and cannot be used for automatic data objects.
This attribute must be used for data declaration and definition at the same time, regardless of whether it is in one file or multiple files.
_ Declspec (thread) cannot be used as a Type modifier.
If no object is defined at the same time as the class declaration, _ declspec (thread) will be ignored. For example:

// Compile with:/LD
_ Declspec (thread) Class X
{
Public:
Int I;
} X; // X is the thread object.
X y; // y is not a thread object.

The following two examples share the same semantics:

_ Declspec (thread) Class B {
Public:
Int data;
} Bobject; // bobject is a thread object.

Class B2 {
Public:
Int data;
};
_ Declspec (thread) B2 bobject2; // bobject2 is the thread object

UUID:
Format: __declspec (UUID ("comobjectguid") declarator
Declare the registered content with a unique identifier as a variable, which can be called with _ uuidof.
The usage is as follows:
Struct _ declspec (UUID ("commandid -0000-0000-c000-000000000046") iunknown;
Struct _ declspec (UUID ("{00020400-0000-0000-c000-000000000046}") idispatch;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.