473,809 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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(i nt& i)
{
i++;
cout << "i is " << i << endl;
}

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

void
register_callba ck(fp fptr)
{
vfptr.push_back (fptr);
}

void
schedule_callba cks(void)
{
int i = 0;
vector<fp>::ite rator 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_callba ck(incr_callbac k);
register_callba ck(decr_callbac k);
schedule_callba cks();
}
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++.m oderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1801
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(i nt& i)
{
i++;
cout << "i is " << i << endl;
}

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

void
register_callba ck(fp fptr)
{
vfptr.push_back (fptr);
}

void
schedule_callba cks(void)
{
int i = 0;
vector<fp>::ite rator 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_callba ck(incr_callbac k);
register_callba ck(decr_callbac k);
schedule_callba cks();
}
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_callba cks() directly.
For more dynamic cases when it is not known at compile time which
functions need to be called by schedule_callba cks(), 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.merke rk(at)dse.nl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. 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_callba cks() directly.
For more dynamic cases when it is not known at compile time which
functions need to be called by schedule_callba cks(), 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::somet hing(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++.m oderated. 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
6244
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 inlined? - Does the C++ standard talk about this? What about gcc 3.x implementation?
14
2792
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 am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
47
3888
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
3159
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 extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
4
1824
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 compile time, definitions of these functions, unlike other definitions, cannot be separately compiled and must be placed in header files. This creates a problem if the compiler does not actually inline a
43
13309
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 compiler. I already compiled the program with Intel Compiler (ICL) on Visual C++, and it works fine and fast. I verified that the functions are really inlined. But with GCC 3.4 (Linux+Cygwin) or ICC (Linux), The same program is about 5
8
1748
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
5069
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
676
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? or is it a call taken by compiler? Second, i see that it is same as what Macro's used to do for c, if so what is the advantage for going in for inline functions than to Macros?
0
9721
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
10376
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
10383
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,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
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
6881
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
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.