473,770 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why no generic function pointers?

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?

Jun 27 '08 #1
32 5679
On Sat, 21 Jun 2008 02:54:25 +0200, copx wrote:
Why doesn't the C standard include generic function pointers?
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.
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.
Jun 27 '08 #2
copx wrote:
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.
And in strictly-conforming C you can cast any function pointer
to any other function pointer type and back again. What's your beef?
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.
If you're irregular, try a laxative.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3

"Nick Bowler" <dr*****@gmail. comschrieb im Newsbeitrag news:g3******** **@rumours.uwat erloo.ca...
On Sat, 21 Jun 2008 02:54:25 +0200, copx wrote:
>Why doesn't the C standard include generic function pointers?

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.


Jun 27 '08 #4

"Eric Sosman" <es*****@ieee-dot-org.invalidschr ieb im Newsbeitrag news:Z_******** *************** *******@comcast .com...
copx wrote:
[snip]
>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.

If you're irregular, try a laxative.
*plonk*
Jun 27 '08 #5
"copx" <co**@gazeta.pl writes:
"Eric Sosman" <es*****@ieee-dot-org.invalidschr ieb im Newsbeitrag news:Z_******** *************** *******@comcast .com...
>copx wrote:
[snip]
>>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.

If you're irregular, try a laxative.

*plonk*
Tons of deliberate personal abuse in this newsgroup, and you plonk
someone over a little joke? I suggest you reconsider -- but if you
don't, it's your loss.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
"copx" <co**@gazeta.pl writes:
Why doesn't the C standard include generic function pointers?
[...]

Remember that there are two distinct ways in which void* is a
"generic" object pointer.

One is that a conversion from foo* to void* and back again is
guaranteed to yield the original value, where foo is any object or
incomplete type. As others have mentioned, *all* function pointers
behave this way. If you want a single generic function pointer type,
I suggest void(*)(void), just because it's the simplest (a typedef
could be helpful).

The other way is that conversions between void* and foo* (foo as
above) may be done implicitly. This isn't the case for function
pointers, but that's just a matter of syntactic convenience.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #7

"copx" <co**@gazeta.pl wrote in message
Why doesn't the C standard include generic function pointers?
Because you can't build an argument list on the fly. You can hardcode

switch(type)
{
case 1:
x = (*fp)(1,2,3);
break;
case 2:
y = (*fp)("My name is fred");
}

but you can't pass in a format string and build a list with an arbitrary
number of integers and strings.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #8
"Malcolm McLean" <re*******@btin ternet.comwrite s:
"copx" <co**@gazeta.pl wrote in message
>Why doesn't the C standard include generic function pointers?
Because you can't build an argument list on the fly.
That begs the question of why one can't do *that*. There are macros
to examine an existing var-arg list, and there the could be macros to
build one from scratch. Given such a thing, a function pointer type
like:

void *(*var_arg_fp)( void *, ...);

would be very flexible (you need a dummy first argument to get the var
arg macros going).

I presume the answer is just history. Of course, it may be that there
are real systems for which such argument building macros/functions
would be impossible, but the seems a stretch.

--
Ben.
Jun 27 '08 #9
"Ben Bacarisse" <be********@bsb .me.ukwrote in message
"Malcolm McLean" <re*******@btin ternet.comwrite s:
>"copx" <co**@gazeta.pl wrote in message
>>Why doesn't the C standard include generic function pointers?
Because you can't build an argument list on the fly.

That begs the question of why one can't do *that*. There are macros
to examine an existing var-arg list, and there the could be macros to
build one from scratch. Given such a thing, a function pointer type
like:

void *(*var_arg_fp)( void *, ...);

would be very flexible (you need a dummy first argument to get the var
arg macros going).

I presume the answer is just history. Of course, it may be that there
are real systems for which such argument building macros/functions
would be impossible, but the seems a stretch.
I think that's right.
Usually any assembly language routine can be translated to C quite easily.
The exception is those assembly routines that build dynamic argument lists.
Normally this is relatively strightforwards in assembly - just push the
first few arguments into registers and the rest on the stack, but you can't
do it in C.

It would tend to open security holes, but I don't think that was the
historical reason for rejecting the idea. Maybe it was because it doesn't
fit well with the structured programming paradigm.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
16460
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a subset of C/C++, thus will have the same syntax as C/C++. Somehow the interpreted code must be able to store generic function pointers because there is no way for the interpreter to know every possible function signature in advance. I was...
0
1384
by: inquisitive | last post by:
Is it possible to have a vector of generic function pointers? I am able to devise a generic function pointer, this way: template <typename elemType, elemType (*function)(std::string&)> struct Method { inline elemType operator()(std::string & property) { return function(property);
1
1369
by: inquisitive | last post by:
Is there a way to create a collection of Method objects, for example a vector or a list? template <typename elemType> struct Method { bool (*function)(elemType&); template <typename elemType> inline bool operator()(elemType & property)
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.