[Usage of __declspec in turn]c++

Source: Internet
Author: User
Tags deprecated uuid

Tag: The value identifier operation is required by constructing the parameter. DLL Archive

The use of __declspec in C + + reprint address: http://www.cnblogs.com/ylhome/archive/2010/07/10/1774770.html
The usage syntax for __declspec in C + + is explained:

__declspec (extended-decl-modifier-seq)

Extension modifiers:

1:align (#)

Use __declspec (#) to precisely control the alignment of user-defined data, #是对齐值.


e.g

__declspec (align (32))
struct str1{
int A, B, C, D, E;
};

"Turn" It is a pair of brothers with the #pragma pack (), which specifies the minimum value for alignment, which specifies the maximum value for alignment. At the same time, the former has high priority. One feature of __declspec (align ()) is that
It only specifies the location of the data alignment, and does not specify the actual memory length of the data, when the specified data is placed in a certain position, the data after the fill is still in accordance with the #pragma pack rules, the actual size of the class/structure and memory pattern of the rule is this: in __ Before Declspec (align ()), data is populated as specified by #pragma pack, as described earlier. When
When encountering __declspec (align ()), first look for the alignment that is closest to the current offset (satisfies the alignment length to max (data itself length, specified value)), and then assigns the specified data type from this
The point begins to fill, and the subsequent data type starts behind it and remains populated with the #pragma pack until the next __declspec (align ()) is encountered. When all the data is filled, the overall structure of the
The values specified by the __declspec (align ()) are compared, whichever is larger as the alignment length of the entire structure. In particular, when __declspec (align ()) specifies a value that is smaller than the length of the corresponding type
, this designation does not work.
2:allocate ("Segname")
Use __declspec (allocate ("Segname")) to declare a data item that has been assigned a data segment. It and #pragma code_seg, CONST_SEG, data_seg,section,init_seg in conjunction 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
Use __declspec (deprecated) to illustrate a function, type, or other identifier that is no longer supported in a new or future version, and 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 ("* * This 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

With __declspec (dllexport), __declspec (dllimport) explicitly defines the DLL interface to the EXE or DLL file that called it, and the function interface is no longer required (. def) for the functions defined by dllexport. Note: If you define a template class in a DLL then it has implicitly made these two declarations, we only need to instantiate when the call, hehe.


E.g in regular mode DLLs
Class ___declspec (dllexport)
testdll{
Testdll () {};
~testdll () {};
};

Called in the client declaration
#import comment (lib, "**.lib")
Class ___declspec (Dllimportt)
testdll{
Testdll () {};
~testdll () {};
};

E.g template class: in DLL
Template<class t>
Class test{
Test () {};
~test () {};
}
Called in the client declaration
int main ()
{
test< int > B;
return 0;
}
5. Jitintrinsic

Use __declspec (jitintrinsic) to mark a function or element as a 64-bit common language runtime. The specific usage is not seen.
6. __declspec (naked)

For a function that is not declared with naked, the General compiler will produce a save field (the compiler generates code to hold the ESI,EDI,EBX,EBP register--prolog when the function is entered) and clears the field (the content--epilog) code that generates code recovery when the function exits, For a function declared with naked, this code is generally not generated, which is very useful for writing device drivers, and we can write a process that supports only x86. The naked is valid only for the function, but not for the type definition. For a function that flags naked, you 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 only for functions, restrict for the function return pointer, restrict that the function return value is not aliased, the returned pointer is unique, no other function pointer alias Flower, that is, the return pointer has not been used is unique. The compiler usually checks to see if the pointer is available and is aliased, is already in use, uses the keyword, and the compiler is not checking the information. Noalias means that a function call cannot modify or reference the visible global state and simply modifies the memory directly pointed to by the pointer parameter. If a function specifies the Noalias keyword, the optimizer considers that only the first level of the parameter pointer is indirectly referenced or modified inside the function except for the argument itself. Visible global state refers to all data sets that are not defined or referenced outside of the encoding range, until they are not available. 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 trust him is not doing some checking operations.


e.g
#include <stdio.h>
#include <stdlib.h>
#define M 800#define N 600#define P 700float * 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 go inline with a specific function.
9. Noreturn__declspec (Noreturn)

Tells the compiler that there is no return value. Note Adding __declspec (noreturn) to a function that you do not want to return causes the error to be undefined.

10.nothrow__declspec (nothrow)

Used for function declarations, which tell the compiler that a 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)

Used in any class of declarations, but only in pure virtual interface classes, so this cannot be instantiated by itself. It prevents the compiler from initializing virtual table pointers when constructing and destructor classes, this removes references to virtual tables that are associated to the class. If you try to instantiate a class that has the novtable keyword, it will happen AV ( Access violation) error. The flaw in C + + is that vtable will increase the size of the code, and this keyword can be used to reduce the size of the code when it does not require an instantiated class or a pure virtual interface.


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 compile 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");
}
};
Role of 12.selectany (RPM)

__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 do so in. h by a similar "__declspec (selectany) type class::variable = value; "Such a code to initialize this global variable. The. h is multiple include, and the linker also rejects multiple definitions of errors for us. What good is this, I think there is a lot of convenience for teamplate programming.


e.g
Class Test
{
Public
static int t;
};
__declspec (selectany) int test::t = 0;
13.thread

Thread is used to declare a thread-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 the executable file or in its own section of the DLL file. The variable following the __declspec (thread) must be declared as a global variable or a static variable in the function (or outside the function). You cannot declare a local variable of type __declspec (thread).


e.g
__declspec (thread)
Class x{
Public
int I;
} x; X is a thread objectx y; Y is not a thread object

14.uuid__declspec (UUID)

A declaration or definition that is 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.:)

[Usage of __declspec in turn]c++

Related Article

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.