473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

[Fwd: inline functions and function pointers]



I was wondering if a function pointer pointing to an inline
function, will actually expand "inline" when the function pointer
is invoked.

#include <iostream>
#include <vector>
using namespace std;

const int MAX_FP_SIZE = 10;
typedef void (*fp)(int& i);
vector<fp> vfptr(0);

inline void incr_callback(int& i)
{
i++;
cout << "i is " << i << endl;
}

inline void decr_callback(int& i)
{
i--;
cout << "i is " << i << endl;
}

void
register_callback(fp fptr)
{
vfptr.push_back(fptr);
}

void
schedule_callbacks(void)
{
int i = 0;
vector<fp>::iterator iter;
fp tmp_fp;
cout << "i is " << i << endl;

for (iter = vfptr.begin(); iter != vfptr.end(); iter++) {
tmp_fp = *iter;
tmp_fp(i);
}
}

int
main(void)
{
register_callback(incr_callback);
register_callback(decr_callback);
schedule_callbacks();
}
In the piece of code above, I would actually like tmp_fp(i) to expand
inline. I understand that such optimization is hard to achieve, but
are there any compilers out there that can do this?

TIA,
Balbir

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1777
Balbir Singh wrote:
I was wondering if a function pointer pointing to an inline
function, will actually expand "inline" when the function pointer
is invoked.

#include <iostream>
#include <vector>
using namespace std;

const int MAX_FP_SIZE = 10;
typedef void (*fp)(int& i);
vector<fp> vfptr(0);

inline void incr_callback(int& i)
{
i++;
cout << "i is " << i << endl;
}

inline void decr_callback(int& i)
{
i--;
cout << "i is " << i << endl;
}

void
register_callback(fp fptr)
{
vfptr.push_back(fptr);
}

void
schedule_callbacks(void)
{
int i = 0;
vector<fp>::iterator iter;
fp tmp_fp;
cout << "i is " << i << endl;

for (iter = vfptr.begin(); iter != vfptr.end(); iter++) {
tmp_fp = *iter;
tmp_fp(i);
}
}

int
main(void)
{
register_callback(incr_callback);
register_callback(decr_callback);
schedule_callbacks();
}
In the piece of code above, I would actually like tmp_fp(i) to expand
inline.
Why?
I understand that such optimization is hard to achieve, but
are there any compilers out there that can do this?


I seriously doubt it. I doubt it is even possible at all. Maybe for
trivial and isolated cases like this one where all definitions are in
the same translation unit it might be _theoretically_ possible. But then
again for trivial examples like this you might as well call
incr_callback() and decr_callback() from schedule_callbacks() directly.
For more dynamic cases when it is not known at compile time which
functions need to be called by schedule_callbacks(), how could the
compiler inline code if it does not know in advance which functions need
to be executed?

Even if it is possible, it appears to be an extremely difficult
optimization that gains close to nothing.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #2
Peter van Merkerk wrote:
I seriously doubt it. I doubt it is even possible at all. Maybe for
trivial and isolated cases like this one where all definitions are in
the same translation unit it might be _theoretically_ possible. But then
again for trivial examples like this you might as well call
incr_callback() and decr_callback() from schedule_callbacks() directly.
For more dynamic cases when it is not known at compile time which
functions need to be called by schedule_callbacks(), how could the
compiler inline code if it does not know in advance which functions need
to be executed?

Even if it is possible, it appears to be an extremely difficult
optimization that gains close to nothing.

In general inlining of functions when they are called through pointers
is difficult to be done.

If you want to do this you have to use static member functions inside a
class.
Example:
class whatever
{
public:
inline static void something(int x)
{
// ...
}
};
template<class T>
void test()
{
int i=1;

// ...

// void whatever::something(int) is inlined here
T::something(i);
}

int main()
{
test<whatever>();
}


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

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

Similar topics

2
by: Abhi | last post by:
Hi, - Are there any cardinal rules as to when a function should not be inlined? - If a function, say f, is being called from multiple other functions/classes, should that function f, be always...
14
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
4
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at...
43
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the...
8
by: John Ratliff | last post by:
Can the compiler ever inline a method when there is a pointer to the member used? Thanks, --John Ratliff
18
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
12
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.