为什么没有泛型函数指针? [英] Why no generic function pointers?

查看:109
本文介绍了为什么没有泛型函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C标准不包含泛型函数指针?


我经常使用函数指针而缺少泛函指针并不是那么酷。


有一个共同的编译器扩展(例如GCC和lccwin32支持)

我认为这是完全合理的:你可以投射各种函数指针

到一个void指针和void指针指向任何类型的函数指针。


这遵循一般的泛型通过void scheme。事实上,它看起来非常像b $ b不规则。对我来说,你不能把函数指针转换为void。


我的意思是,当然,泛型函数指针是危险的,因为它们允许

你要调用一个带有错误参数的函数,编译器无法检测到它。

但它不再是危险的比无符号的char指针指向任何类型的数据,即

不对抗C精神。 IMO。


实际上,K& R编译器支持半通用函数指针IIRC。你是

能够省略参数声明(但不是返回类型声明),例如

int(* foo)();


那么为什么委员会决定不制作通用函数指针?

Why doesn''t the C standard include generic function pointers?

I use function pointers a lot and the lack of generic ones is not so cool.

There is a common compiler extension (supported by GCC and lccwin32 for example)
which I consider to be perfectly reasonable: you can cast every kind of function pointer
to a void pointer and void pointers to any kind of function pointer.

This follows the general "generics through void scheme" of C. In fact, it seems to be quite
"irregular" to me that you can''t cast function pointers to void.

I mean, of course, generic function pointers are "dangerous", because they allow
you to call a function with bad arguments and the compiler can''t detect that.
But it''s not any more "dangerous" than unsigned char pointers to any type of data i.e.
not against the "C spirit" IMO.

In fact, K&R compilers supported semi-generic function pointers IIRC. You were
able to leave out the parameter declaration (but not the return type declaration) e.g.
int (*foo)();

So why did the comittee decide against making generic function pointers standard?

推荐答案

2008年6月21日星期六02: 54:25 +0200,copx写道:
On Sat, 21 Jun 2008 02:54:25 +0200, copx wrote:

为什么C标准不包含泛型函数指针?
Why doesn''t the C standard include generic function pointers?



它确实在某种程度上:你可以转换(使用强制转换)一个函数指针,指向

不同的函数指针类型并返回再次:结果将比较

等于原始指针。这类似于

void *的语义,除了转换不是隐含的。

It does, in a way: you can convert (using a cast) a function pointer to a
different function pointer type and back again: the result shall compare
equal to the original pointer. This is similar to the semantics of
void *, except that the conversion is not implicit.


这遵循一般的泛型无效计划事实上,

似乎是非常不规则的。对我而言,你不能施展功能

指向虚空。
This follows the general "generics through void scheme" of C. In fact,
it seems to be quite "irregular" to me that you can''t cast function
pointers to void.



在某些系统上,函数指针明显大于对象

指针。

On some systems, function pointers are significantly larger than object
pointers.


实际上,K& R编译器支持半泛型函数指针IIRC。

你能够省略参数声明(但不是返回

类型声明)例如int(* foo)();
In fact, K&R compilers supported semi-generic function pointers IIRC.
You were able to leave out the parameter declaration (but not the return
type declaration) e.g. int (*foo)();



你仍然可以这样做。

You can still do this.


copx写道:
copx wrote:

为什么C标准不包含泛型函数指针?


我经常使用函数指针而缺少泛函指针并不是那么酷。 />

有一个共同的编译器扩展(例如GCC和lccwin32支持)

我认为是完全合理的:你可以抛出各种函数指针

到一个void指针和void指针到任何类型的函数指针。
Why doesn''t the C standard include generic function pointers?

I use function pointers a lot and the lack of generic ones is not so cool.

There is a common compiler extension (supported by GCC and lccwin32 for example)
which I consider to be perfectly reasonable: you can cast every kind of function pointer
to a void pointer and void pointers to any kind of function pointer.



在严格符合C的情况下,您可以将任何函数指针

转换为任何其他函数指针类型,然后再返回。什么是你的牛肉?

And in strictly-conforming C you can cast any function pointer
to any other function pointer type and back again. What''s your beef?


这遵循一般的通用无效方案事实上,它看起来非常像b $ b不规则。对我来说,你不能把函数指针强制转换为void。
This follows the general "generics through void scheme" of C. In fact, it seems to be quite
"irregular" to me that you can''t cast function pointers to void.



如果你不规律,可以试试泻药。


-

Eric索斯曼
es*****@ieee-dot-org.inva lid

If you''re irregular, try a laxative.

--
Eric Sosman
es*****@ieee-dot-org.invalid




" Nick Bowler" < dr ***** @ gmail.comschrieb im Newsbeitrag新闻:g3 ********** @ rumours.uwaterloo.ca ...

"Nick Bowler" <dr*****@gmail.comschrieb im Newsbeitrag news:g3**********@rumours.uwaterloo.ca...

On Sat,2008年6月21日02:54:25 +0200,copx写道:
On Sat, 21 Jun 2008 02:54:25 +0200, copx wrote:

>为什么C标准不包含泛型函数指针?
>Why doesn''t the C standard include generic function pointers?



它确实在某种程度上:你可以转换(使用强制转换)一个函数指针,指向

不同的函数指针类型并返回再次:结果将比较

等于原始指针。这类似于

void *的语义,除了转换不是隐式的。


It does, in a way: you can convert (using a cast) a function pointer to a
different function pointer type and back again: the result shall compare
equal to the original pointer. This is similar to the semantics of
void *, except that the conversion is not implicit.



有趣。谢谢,我不知道。


Interesting. Thanks, I didn''t know that.



这篇关于为什么没有泛型函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆