473,785 Members | 2,816 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
32 5681
Ben Bacarisse skrev:
Tor Rustad <to********@hot mail.comwrites:
>Ben Bacarisse skrev:
[...]
>>Just to be clear, I was focusing on the function call. At that point,
there is not enough information for the compiler to generate a correct
call, at least not in the most general case.
The compiler can very much generate the correct call, BUT it's a
different matter if such a function exist, and this is the job of the
run-time loader to detect.

I was making a narrow technical point: given a generic function
pointer type, a C function call does not have the information required
to check the type and generate the correct calling code. Assume fp is
such a generic pointer. What is the type of function being called
here:

fp(12.3);
Ohh..no

we can't do pointer arithmetic on pointer-to-void either:

void foo(void *p)
{
(*(char *)p)++;
}

so I don't see why you assume I somehow suggest that calling a "generic
function pointer" to be meaningful.
Converting a function pointer to some "generic function pointer", would
throw away information, while converting a "generic function pointer" to
a function pointer, would add information.

I don't see why such conversion should be explicit (i.e. require a
cast), other than as providing a comment for humans (that is, if typedef
of the function pointer is used, else not very readable).
Hmm.. another possibly useful thing with a "generic function pointer",
could be to hold functions (of different types) in a jump table:

gen_func_t jmp[3] = {f1, f2, f3};
Anyway, this is rather academic... in ca. 99,99% IRL cases, void* will
work fine as a generic function pointer. :)

--
Tor <bw****@wvtqvm. vw | tr i-za-h a-z>
Jun 27 '08 #31
Tor Rustad <bw****@wvtqvm. vwwrites:
Ben Bacarisse skrev:
>Tor Rustad <to********@hot mail.comwrites:
>>Ben Bacarisse skrev:

[...]
>>>Just to be clear, I was focusing on the function call. At that point,
there is not enough information for the compiler to generate a correct
call, at least not in the most general case.
The compiler can very much generate the correct call, BUT it's a
different matter if such a function exist, and this is the job of the
run-time loader to detect.

I was making a narrow technical point: given a generic function
pointer type, a C function call does not have the information required
to check the type and generate the correct calling code. Assume fp is
such a generic pointer. What is the type of function being called
here:

fp(12.3);

Ohh..no

we can't do pointer arithmetic on pointer-to-void either:

void foo(void *p)
{
(*(char *)p)++;
}

so I don't see why you assume I somehow suggest that calling a
"generic function pointer" to be meaningful.
I wasn't. I was trying to explain what I was saying. It may well
have had nothing to do with what you were saying -- these things
happen on Usenet! That is why I asked for an example of what you
want...
Converting a function pointer to some "generic function pointer",
would throw away information, while converting a "generic function
pointer" to a function pointer, would add information.

I don't see why such conversion should be explicit (i.e. require a
cast), other than as providing a comment for humans (that is, if
typedef of the function pointer is used, else not very readable).
Now I see what you are saying. You want a pointer type to (and from)
which any function pointer can be converted without a cast. This
would help a little, I agree.

I though you were suggesting that the forced cast somehow prevented
some type checking that could otherwise be done.

--
Ben.
Jun 27 '08 #32
"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
"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.
For some compilers, functions with varadic argument lists and other function
types can be treated differently.

For instance, I can tell one compiler that I want to pass arguments as
registers when possible. If the function is varadic, then that convention
will not be used (and it will definitely cause problems if a prototype is
not in scope and the warning is ignored).

So I think it is up to the compiler vendor to decide exactly how generic a
pointer to function type might be.
** Posted from http://www.teranews.com **
Jun 27 '08 #33

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

Similar topics

28
16463
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
1385
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
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
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
10152
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
10092
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,...
1
7500
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
6740
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.