468,103 Members | 1,387 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

[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 1621
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Abhi | last post: by
14 posts views Thread by Chris Mantoulidis | last post: by
47 posts views Thread by Richard Hayden | last post: by
20 posts views Thread by Grumble | last post: by
4 posts views Thread by Tony Johansson | last post: by
43 posts views Thread by Patrick Laurent | last post: by
8 posts views Thread by John Ratliff | last post: by
18 posts views Thread by Method Man | last post: by
12 posts views Thread by sam_cit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.