C + + in the use of __declspec as follows, want to understand the continue to look down.
Syntax Description:
__declspec (extended-decl-modifier-seq)
Extension modifiers:
1:align (#)
Use __declspec (align (#)) to precisely control the alignment of user-defined data, #是对齐值.
e.g
__declspec (align)
struct str1{
int A, B, C, D, E;
};
It is a pair of brothers with the #pragma pack (), which sets the minimum alignment value, which prescribes the maximum alignment. When appearing at the same time, the former has high priority.
A feature of __declspec (align ()) is that it simply specifies the location of the data alignment, without specifying the amount of memory that the data actually occupies, and when the specified data is placed in a determined position, the subsequent data fill is still populated in the manner specified by the #pragma pack, The actual size of the class/structure and the rules of the memory pattern are as follows: Before __declspec (align ()), the data is populated in the manner specified by the #pragma pack, as described earlier.
When you meet __declspec (align ()), first look for the nearest alignment to the current offset (the length of the alignment is Max (the data itself), and then the specified data type starts at this point, followed by the data type from behind it, still following the # Pragma pack fills until the next __declspec (align ()) is encountered. When all the data is filled, compare the overall alignment value of the structure with the values specified in __declspec (align ()), and take the larger alignment length as the entire structure. In particular, this designation does not work when the __declspec (align ()) specifies a value that is smaller than the corresponding type length.
2:allocate ("Segname")
Declare a data item that has been assigned a data segment with __declspec (Allocate ("Segname")). It and #pragma code_seg, const_seg, data_seg,section,init_seg with the use of, segname must have these Dongdong declaration.
e.g
#pragma data_seg ("share_data")
int a = 0;
int b;
#pragma data_seg () __declspec (allocate ("Share_data")) int c = 1;
__declspec (Allocate ("Share_data")) int D;
3. Deprecated
Using __declspec (deprecated) to illustrate that a function, type, or other identifier is no longer supported in a new version or future version, you should not use this function or type. It has the same effect as #pragma deprecated.
e.g
#define MY_TEXT "function is deprecated"
void func1 (void) {}
__declspec (deprecated) void func1 (int) {printf (" FUNC1N ");}
__declspec (Deprecated ("* * is a deprecated function * *")) void Func2 (int) {printf ("func2n");}
__declspec (deprecated (my_text)) void func3 (int) {printf ("func3");}
int main ()
{
fun1 ();
Fun2 ();
Fun3 ();
}
4.dllimport and Dllexport
Using __declspec (dllexport), __declspec (dllimport) explicitly define the DLL interface to invoke its EXE or DLL file, the function defined by the dllexport no longer requires (. def) files to declare these function interfaces. Note: If the template class is defined in the DLL it has implicitly made these two declarations, we just need to instantiate the call, hehe.
E.g in a normal way DLL
Class ___declspec (dllexport)
testdll{
Testdll () {};
~testdll () {};
};
The calling client declares
#import comment (lib, **.lib)
class ___declspec (Dllimportt)
testdll{
Testdll () {};
~testdll () {};
};
E.g template class: DLL
Template<class t>
class test{
Test () {};
~test () {};
}
The calling client declares
int main ()
{
test< int > b;
return 0;
5. Jitintrinsic
__declspec (jitintrinsic) is used to mark a function or element as a 64-bit common language runtime. The specific usage was not seen.
6. __declspec (naked)
For functions that are not declared with naked, a generic compiler produces a save site (the compiler generates code to hold the ESI,EDI,EBX,EBP register--prolog) and clears the scene (when exiting the function, it generates code to restore the contents of those registers--epilog) code, For functions declared with naked, this code is not normally generated, and this property is useful for writing device drivers, and we can write a process that supports only x86. Naked is valid only for functions, but not for type definitions. A function that flags a naked cannot produce an inline function that immediately uses the __forceinline keyword.
e.g
__declspec (naked) func ()
{
int i;
Int J;
__ASM/* Prolog */
{
push ebp
mov ebp, esp
Sub ESP, __local_size
}/
* Function body */
__ASM/* Epilog/
{
mov esp, ebp
pop ebp
ret
}
7. Restrict and Noalias
__declspec (restrict) and __declspec (noalias) are used to improve program performance and optimize programs. These two keywords are used only for functions, restrict for function return pointers, restrict the return value of the function is not aliased, the returned pointer is unique, not by another function pointer alias Flower, that is, the return pointer has not been used is unique. The compiler typically checks to see if the pointer is available and is aliased, is already in use, and uses this keyword, and the compiler is not checking the information. Noalias means that a function call cannot modify or reference a visible global state and simply modify the memory that the pointer parameter directly points to. If a function specifies the Noalias keyword, the optimizer considers that except for the parameter itself, only the first level of the parameter pointer is indirectly referenced or modified within the function. A visible global state is a complete set of data that is not defined or referenced outside the encoding range, and cannot be obtained until it is not. The encoding range refers to all source files or individual source files. In fact, these two keywords is to give the compiler a guarantee that the compiler trusts him not to do some checking operation.
e.g
#include <stdio.h> #include <stdlib.h> #define M 800#define N 600#define P 70
0float * mempool, * MEMPTR;
__declspec (restrict) float * MA (int size) {float * retval;
retval = memptr;
Memptr + = size;
return retval;
__declspec (restrict) float * init (int m, int n) {float * A;
int I, J;
int k=1;
A = MA (M * n);
if (!a) exit (1);
For (i=0, i<m; i++) for (j=0; j<n; j + +) a[i*n+j] = 0.1/k++;
return A;
} __declspec (noalias) void multiply (float * A, float * b, float * c) {int I, j, K;
For (J=0-j<p; J + +) for (i=0; i<m; i++) for (k=0; k<n; k++) C[i * p + j] = A[i * N + K] * B[k * p + j];
int main () {float * A, * b, * C;
Mempool = (float *) malloc (sizeof (float) * (m*n + n*p + m*p)); if (!mempool) puts ("Error:malloc returned null");
Exit (1);
Memptr = Mempool;
A = init (M, N);
b = init (N, P);
c = init (M, P);
Multiply (A, B, c); }
8. Noinline__declspec (Noinline)
Tells the compiler not to inline a specific function.
9. Noreturn__declspec (Noreturn)
Tells the compiler that there is no return value. Note that adding __declspec (noreturn) to a function that you do not want to return will result in an undefined error.
10.nothrow__declspec (nothrow)
For a function declaration, which tells the compiler that the function does not throw an exception.
e.g
#define WINAPI __declspec (nothrow) __stdcall
void WINAPI F1 ();
void __declspec (nothrow) __stdcall F2 ();
void __stdcall F3 () throw ();
11.novtable __declspec (novtable)
Use a declaration of any class, but only in a pure virtual interface class, so such a thing cannot be instantiated by itself. It prevents the compiler from initializing a virtual table pointer when constructing and destructor a class, which removes a reference to the virtual table associated with the class. If you try this instantiate a class with the Novtable keyword, it will take place AV ( Access violation) error. The flaw of virtual in C + + is that vtable will increase the size of the code, when you do not need to instantiate a class or a pure virtual interface, use this keyword to reduce the size of the code.
e.g
#if _msc_ver >= 1100 &&!defined (_DEBUG)
#define Afx_novtable __declspec (novtable)
#else
# Define Afx_novtable
#endif ...
.
Class Afx_novtable CObject
{
...
};
This is the VC inside a piece of code, we can see compiled release version, before CObject is __declspec (novtable), in the debug version does not have this limitation.
e.g
#include <stdio.h>
struct __declspec (novtable) X
{
virtual void mf ();
};
struct Y:public X
{
void mf ()
{
printf_s ("in Yn");
}
;
The role of 12.selectany
__declspec (selectany) allows us to initialize a global variable in the. h file instead of only in. cpp. For example, there is a class, which has a static variable, so we can pass a similar "__declspec" (selectany) Type class::variable = value in. h. "Such code to initialize this global variable. If this. h is being include multiple times, the linker will also remove multiple-defined errors for us. What is the advantage of this, I think for teamplate programming will have a lot of convenience.
e.g
Class Test
{public
:
static int t;
};
13.thread
Thread is used to declare a threaded local variable. The prefix for __declspec (thread) is a modifier that Microsoft adds to the Visual C + + compiler. It tells the compiler that the corresponding variable should be placed in its own section in the executable file or DLL file. A variable after __declspec (thread) must be declared as a global or static variable in a function (or outside of a function). You cannot declare a local variable of type __declspec (thread).
e.g
__declspec (thread)
class x{public
:
int I;
14.uuid__declspec (UUID)
A declaration or definition used by the compiler to associate a GUID to a class or struct with a UUID attribute.
E.gstruct __declspec (uuid ("00000000-0000-0000-c000-000000000046")) Iunknown;struct __declspec (uuid ("{ 00020400-0000-0000-c000-000000000046} ") IDispatch; we can view the source code in MFC.:)