首页 > 代码库 > c++概要总结,持续更新
c++概要总结,持续更新
一、 base data types(atom types)
from c++0x/c++2011standard.
typename | g++4.5-32bit | g++4.5-64bit | vc10.0-32bit | vc10.0-64bit |
char | 1 | 1 | 1 | 1 |
char16_t | 2 | 2 | 2 | 2 |
char32_t | 4 | 4 | 4 | 4 |
wchar_t | 4 | 4 | 2 | 2 |
short | 2 | 2 | 2 | 2 |
int | 4 | 4 | 4 | 4 |
long | 4 | 8 | 4 | 4 |
long long | 8 | 8 | 8 | 8 |
float | 4 | 4 | 4 | 4 |
double | 8 | 8 | 8 | 8 |
long double | 12 | 16 | 8 | 8 |
派生成员函数指针(单继承) | 8 | 16 | 4 | 8 |
派生成员函数指针(多继承) | 8 | 16 | 8 | 16 |
派生成员函数指针(虚继承) | 8 | 16 | 12 | 16 |
二、 definefunction
#ifdef __linux__
#define__stdcall __attribute__((__stdcall__))
#define__thiscall __attribute__((__thiscall__))
#define__cdecl __attribute__((__cdecl__))
#define__fastcall __attribute__(__fastcall__))
#endif
RetTypecall_convFuncName(ParamTypeparam);
example: RecursiveCall
int __cdeclcalc_sum(int sum,inti)
{
if( 0 == i )
{
return sum;
}
sum += i;
returncalc_sum(sum, --i);
}
三、 control statement
if( cond1)
{
statement1;
}
else if(cond2 )
{
statement2;
}
else
{
statement3;
}
四、 switch usage
switch(flg )
{
case 1:
statement1;
break;
case2:
statement2;
break;
default:
statement3;
}
五、 loopstatement
while(condition )
{
statements;
}
do{
statements;
}while( condition );
for( ;condition; )
{
statements;
}
loop:
statements;
if( condition )
{
goto loop;
}
六、 pitfallsand knowledge
(1)thinking:
int i =1;
int j =(++i) + (i++) + (++i);
VC6.0/GNUgcc/g++: i = 4, j = 7
VS2005/2008/2010: i = 4, j = 9;
(2)thedifference of following statements:
char* lpszStr1 = "hello world";
const char* lpszStr2 = "helloworld";
char lpszStr3[] = "helloworld";
(3)thedifferent of pointer and reference
int i=0;
int& j=i;
int* k=&i;// int* k=&j;
ref,alias(the same entity) pointer,address(addressof entity)
In fact, the implement of pointer and referenceby assembly is the same. Such as following:
int i = 5;
int* pi = &i;
int ri = i;
The corresponding assembly code:
mov dword [i], 5
lea eax, [i]
mov dword ptr[pi], eax;
lea eax, dword ptr[i]
mov dword ptr[ri], eax
(4) enumtype
4 bytessigned in 32bit-program
8 bytes signed in 64bit-program
(5) macro
#define parse_to_string(macro_arg) #macro_arg
#define contact(arg1, arg2) arg1##arg2
(6)templatespecialization in class.
VisualStudio support explicit and implicitspecialization
GCC just support implicit specialization.
七、 c++0xstandard
vs2010 introduce:
1.lambda expression
genericsyntax:
[](ParaTypepara) ->RetType//->RetType: specific return type
{
statements;
}
[] : lambdaexpr prefix
othersyntax:
(1)
intlocal= 0;
intresult= [&](void) ->int
{
return ++local;
}();
or
intresult= [&local](void) ->int
{
return ++local;
}();
(2)
intlocal= 0;
intresult= [=](void)mutable ->int
{
return ++local;
}();
or
intresult= [local](void)mutable->int
{
return ++local;
}();
2.static_assert
example:
template<typename_Ty, typename _Tx>
_Typointer_cast(_Tx sptr)
{
static_assert(std::tr1::is_pointer<_Tx>::value&&
std::tr1::is_pointer<_Ty>::value,
“the type of src and dstoperand must be pointer”);
union{
_Txs_ptr;
_Tyd_ptr;
} uData;
uData.s_ptr = sptr;
returnuData.d_ptr;
}
3.support new means for keyword: auto, type deduction.
example:
autoi =3; // be similar to var in C#
4.rvalue, it can bind on a temporary.
example:
template<typenameT>
classInteger
{
public:
Integer(void)
{
this->data = http://www.mamicode.com/T();
}
Integer(constT&data)
{
this->data = http://www.mamicode.com/data;
}
Integer(constInteger&integer)
{
this->data = http://www.mamicode.com/integer.data;
}
bool operator==(constInteger&integer)
{
return this->data =http://www.mamicode.com/= integer.data;
}
bool operator==(const Integer&&integer)
{
return *this == integer;
}
private:
T data;
};
5.decltype usage:
auto i =1;
auto j =1.0f;
typedefdecltype(j) JType;
gcc 4.5 ext support
6.new keywords: char16_t, char32_t
example:
char16_tf[]= u”hello world!”;
char32_te[]= U”hello world!”;
八、 inlineasm usage
Win32vc6.0, 8.0, 9.0, 10.0
int a = 3, b = 0;
__asm moveax, a;
__asm movb, eax;
Linux32
inta =3, b = 0;
__asm__[__volatile__](“movl %1, %0” : “=r”(b) : “m” (a));
or
asm[volatile](“movl%1, %0” : “=r”(b) : “m”(a));
example:
template<typename bchar>
__declspec(naked)
int __cdecl _strlen(const bchar*)
{
__asm
{
movesi, dword ptr[esp + 4];
moveax, -1;
cmpesi, 0;
jzend__;
label:
movcl, byte ptr [esi];
incesi;
inceax;
cmpcl, 0;
jnzlabel;
end__:
ret;
}
}
template<typename wchar>
__declspec(naked)
int __cdecl _wcslen(const wchar*)
{
__asm
{
movesi, dword ptr[esp + 4];
moveax, -1;
cmpesi, 0;
jzend;
label:
movcx, word ptr [esi];
incesi;
inceax;
cmpcx, 0;
jnzlabel;
end:
ret;
}
}
template<typename bchar>
__declspec(naked)
char* __cdecl_strcpy(bchar*,const bchar*, int)
{
__asm
{
movecx, dword ptr[esp + 12]
movesi, dword ptr[esp + 8];
movedi, dword ptr[esp + 4];
incecx;
repmovsb;
moveax, edi;
subeax, dword ptr[esp + 12];
deceax;
ret;
}
}
template<typename wchar>
__declspec(naked)
wchar_t* __cdecl_wcscpy(wchar*,const wchar*, int)
{
__asm
{
movecx, dword ptr[esp + 12]
movesi, dword ptr[esp + 8];
movedi, dword ptr[esp + 4];
incecx;
repmovsw;
moveax, edi;
subeax, dword ptr[esp + 12];
subeax, dword ptr[esp + 12];
subeax, 2;
ret;
}
}
template<typename bchar>
__declspec(naked)
char* __cdecl_strcat(bchar*,int, constbchar*,int)
{
__asm
{
movecx, dword ptr[esp + 16];
movesi, dword ptr[esp + 12];
movedx, dword ptr[esp + 8];
movedi, dword ptr[esp + 4];
addedi, edx;
repmovsb;
movbyte ptr[edi], 0;
moveax, edi;
subeax, dword ptr[esp + 16];
subeax, edx;
ret;
}
}
template<typename wchar>
__declspec(naked)
wchar_t* __cdecl_wcscat(wchar*,int, constwchar*,int)
{
__asm
{
movecx, dword ptr[esp + 16];
movesi, dword ptr[esp + 12];
movedx, dword ptr[esp + 8];
addedx, dword ptr[esp + 8];
movedi, dword ptr[esp + 4];
addedi, edx;
repmovsw;
movword ptr[edi], 0;
moveax, edi;
subeax, dword ptr[esp + 16];
subeax, dword ptr[esp + 16];
subeax, edx;
ret;
}
}