C与泛型

【问题描述】泛型是一种特殊的类型,它把指定类型的工作推迟到客户端代码声明并实例化类或方法的时候进行。泛型旨在解决函数名字冲突的问题。一般认为泛型是高级语言的能力,泛型的实现一般借助高级语言的模板概念。C语言是否能实现泛型呢?答案是能,不过比高级语言困难得多。下面总结两种泛型的实现方法。

【解析】

编译环境:

Fedora 10, gcc版本gcc4.3.2

1 利用函数指针实现泛型

【代码清单】

printtest.c

#include <stdio.h>
#include <stdlib.h>

#define DEBUG 0

#define PRINTSTAT( FORMAT, STAT) \
	printf("**************** " FORMAT " Test ****************\n"\
	,STAT);

#define DEBUG_PRINT(FORMAT, VALUE) printf("File %s line %d: "\
			#VALUE " = " FORMAT "\n"\
			,__FILE__, __LINE__,VALUE\
			);

enum {
	RET_OK,
	RET_FAIL
};

typedef int (*FXPrintFun)(void *data);

int print(FXPrintFun _print, void *data)
{
	return _print(data);
}

static int print_int(void *data)
{
	printf("%d", (int)data);

	return RET_OK;
}

static int print_int2(void *data)
{
	printf("%d", *(int*)data);

	return RET_OK;
}

static int print_float(void *data)
{
	printf("%f", *(float*)data);

	return RET_OK;
}

static int print_str(void *data)
{
	printf("%s", (char*)data);

	return RET_OK;
}

int main(void)
{
	int i = 0;
	float f = 2.6;
	char *test = "Generics test!";

	int *pi = &i;
	float *pf = &f;

#if DEBUG
	DEBUG_PRINT("%f", f)
#endif

	PRINTSTAT( "%s", "Integer")
	for(; i<10; i++)
		print(print_int, (void *)i);
	printf("\n");

	PRINTSTAT( "%s", "Integer")
	for(i = 0; i<10; i++)
		print(print_int2, (void *)pi);
	printf("\n");

	PRINTSTAT( "%s", "Float")
	print(print_float, (void *)pf);
	printf("\n");

	PRINTSTAT( "%s", "String")
	print(print_str, (void *)test);
	printf("\n");

	return RET_OK;
}


Makefile

OBJS = printtest.o
TARGET = printtest
SRC = printtest.c

all:$(OBJS)
	gcc -g $(OBJS) -o $(TARGET)
$(OBJS):printtest.s
	gcc -g -c printtest.s -o $(OBJS)
printtest.s:printtest.i
	gcc -g -S printtest.i -o printtest.s
printtest.i:$(SRC)
	gcc -g -E $(SRC) -o printtest.i
clean:
	rm *~ *.o *.s *.i $(TARGET)

*此处为了观察代码的编译、汇编、链接过程,Makefile稍显复杂,可直接用下述指令编译

gcc printtest.c -o printtest

 

【运行结果】

**************** Integer Test ****************
0123456789
**************** Integer Test ****************
0123456789
**************** Float Test ****************
2.600000
**************** String Test ****************
Generics test!

 


上述代码,有一处值得注意,在定义时,提供了两种print_int函数:

static int print_int(void *data)
{
	printf("%d", (int)data);

	return RET_OK;
}

static int print_int2(void *data)
{
	printf("%d", *(int*)data);

	return RET_OK;
}

调用时:

int i = 0;

int *pi = &i;

PRINTSTAT( "%s", "Integer")
for(; i<10; i++)
	print(print_int, (void *)i);
printf("\n");

PRINTSTAT( "%s", "Integer")
for(i = 0; i<10; i++)
	print(print_int2, (void *)pi);
printf("\n");


调用print_int时,将int类型强制转换为void *类型;调用print_int2时,将int *类型强制转化为void *类型。令人惊讶的是,上述两种方法,都能输出正确结果。其实,print_int提供的方法只是凑巧而已,因为int是4字节,而void *类型也是4字节,强制转换居然没有发生错误!但如果是float类型,再利用print_int类似的方法就不行了,gcc提示无法进行转换。安全的方式是将int *转换为void *,再在print_int处理函数中将void *转换为int *,然后解引用。

 

2 采用“#”

方法1提供的代码中,已经看出了“#”的威力。下面看看泛型的另一种实现:

【代码清单】

printtest.h

#ifndef __PRINTTEST_H__
#define __PRINTTEST_H__

#define GNERIC_PRINT(TYPE,FORMAT,SUFFIX) \
	int print##SUFFIX( TYPE data )\
	{\
	printf(FORMAT, data);\
	return 0;\
	}

#endif 


printtest.c

#include <stdio.h>
#include <stdlib.h>
#include "printtest.h"

#define DEBUG 0

#define PRINTSTAT( FORMAT, STAT) \
	printf("**************** " FORMAT " Test ****************\n"\
	,STAT);

#define DEBUG_PRINT(FORMAT, VALUE) printf("File %s line %d: "\
			#VALUE " = " FORMAT "\n"\
			,__FILE__, __LINE__,VALUE\
			);

enum {
	RET_OK,
	RET_FAIL
};

GNERIC_PRINT(int, "%d", _int)
GNERIC_PRINT(float, "%f", _float)
GNERIC_PRINT(char *, "%s", _str)

int main(void)
{
	int i = 0;
	float f = 2.6;
	char *test = "Generics test!";

#if DEBUG
	DEBUG_PRINT("%f", f)
#endif

	PRINTSTAT( "%s", "Integer")
	for(; i<10; i++)
		print_int(i);
	printf("\n");

	PRINTSTAT( "%s", "Float")
	print_float(f);
	printf("\n");

	PRINTSTAT( "%s", "String")
	print_str(test);
	printf("\n");

	return RET_OK;
}


Makefile

OBJS = printtest.o
TARGET = printtest
SRC = printtest.c

all:$(OBJS)
	gcc -g $(OBJS) -o $(TARGET)
$(OBJS):printtest.s
	gcc -g -c printtest.s -o $(OBJS)
printtest.s:printtest.i
	gcc -g -S printtest.i -o printtest.s
printtest.i:$(SRC)
	gcc -g  -E $(SRC) -o printtest.i
clean:
	rm *~ *.o *.s *.i $(TARGET)

*SRC = printtest.c,而不是SRC=printtest.c printtest.h。后者,不能正确完成预处理,此处颇让人费解。有人能说明原因吗?


或采用

gcc printtest.h printtest.c -o printtest

 进行编译

 

【运行结果】

**************** Integer Test ****************
0123456789
**************** Float Test ****************
2.600000
**************** String Test ****************
Generics test!

 

这种方法看似很炫,但在代码量巨大的情况下,非常容易出错。因此推荐采用函数指针的形式实现泛型。 


 

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值