#include "stdafx.h"
#include <stdlib.h>
using namespace std;
template<class ToT>
int getTypeSize(ToT *t,int len=1);
#define GET_TYPE_LEN(TYPE_NAME) getTypeSize(new TYPE_NAME());
struct MM{
int m1;
char m2;
long m3;
char* m4;
};
int _tmain(int argc, _TCHAR* argv[])
{
struct MM mm;
int nLen1 = getTypeSize(&mm);
int nLen2 = GET_TYPE_LEN(MM);
return 0;
}
template<class ToT>
int getTypeSize(ToT *t, int len)
{
return((char *)(t+1)-(char *)t)*len;
}
这种实现,有几个问题:
1、区分是类型还是变量,暂时没有找到好的办法
2、对于数组,暂时没有好的方法处理
3、为简化处理,要求输入为指针
加油,向你看齐,我看好你哦