定义一个函数模板,为指定的数据类型分配内存。
实例如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct _TEST
{
int n;
char buf[100];
}TEST;
template <typename T>
T* fun()
{
return (T*)malloc(sizeof(T));
}
int main()
{
TEST* p = 0;
p = fun<TEST>();
printf("p = %d\n", p);
free(p);
return 0;
}