Advertisement

Pointers to inline functions?

Started by February 28, 2000 08:21 AM
5 comments, last by Yanroy 24 years, 6 months ago
Can it be done? Logically, I would say no, because the function is inserted into the source code.... but that seems kinda stupid to me... does anyone know the real answer? --------------------

You are not a real programmer until you end all your sentences with semicolons;

"Very funny Scotty, now beam down my clothes." www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

think of it this way: can you make a pointer to a macro?

Nope.
Advertisement
You can't compare the situation to a macro. It's up to the compiler whether or not to inline any particular function at any particular instance of the function occurance. Within the same program you may have the function inlined in some spots, but called in other spots. Therefore if the compiler finds a pointer to the function, it will like emit the code for the function so that the function can have an address and inline happily elsewhere. For example, the following code compiles and runs as you would expect if the inline keyword wasn't there:

#include <stdio.h>inline void p(void) {  printf("hello\n");}int main(int argc, char ** argv) {  void (*func)(void);  p();  func = p;  func();}  

Compiler version: gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

The emitted code has the p inlined in the first instance, and a function call off the pointer in the func() call.

Post editted to get the #include to show up.

Edited by - SiCrane on 2/28/00 10:47:54 AM
And what, may I ask, is the point of pointing to an inline function? Other than to FORCE non-inlining, which defeats the purpose of the whole inline keyword, right? Your syntax is correct, I just don''t see a point to it.

As I said, the only apparent use is to force non-inlining of an inline function. Why would you want to do that? To have a "slow" and a "fast" version of the same function?

BTW I thought the inline keyword qualified a function in the same was as a const or an int , but I guess I was wrong.
Well, Yanroy asked "Can it be done?" not "Should it be done?"

As for the point... Well, the compiler will still inline it if it''s short enough in places where it''s called directly. So if you have a short function that you need in many places, you can inline it to get it, well, inlined for performance. However you can use function pointers to do odd control forms. Say you have an array of function pointers and choose one by index number, then you can still stick a pointer to the inlined function in there. Or pass the function pointer as a continuation. Or whatever other twisted things that people do with function pointers.
Having a virtual function that is declared inline
(i.e. defined in the class declaration) will
generate a similar situation. In cases where the
function is called on instance of this class, the
compiler can''t just substitute the inline code because
it needs to be able to deal with overriding this
function in subclasses. However, in the case where
this function is called explicitly (such as in an
overriding function that calls BaseClass::FuntionName
explicitly) it can still use the inline code. I''m
guessing the handling of these similar situations
is extremely compiler dependent.
Advertisement
quote: Original post by SiCrane

Well, Yanroy asked "Can it be done?" not "Should it be done?"

...
However you can use function pointers to do odd control forms. Say you have an array of function pointers and choose one by index number, then you can still stick a pointer to the inlined function in there. Or pass the function pointer as a continuation. Or whatever other twisted things that people do with function pointers.


You hit that nail right on the head! The function in question is for blitting certian types of objects, but I need to have a pointer to it for use in my game''s pseudo-GUI. Sometimes it needs to be inline, sometimes it needs to be a pointer.



--------------------


You are not a real programmer until you end all your sentences with semicolons;
www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

This topic is closed to new replies.

Advertisement