[Fwd: inline functions and function pointers]

B

Balbir Singh

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
 
P

Peter van Merkerk

Balbir said:
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.
 
I

Ioannis Vranos

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top