473,395 Members | 1,631 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,395 software developers and data experts.

Function Pointers and Inlining

If I call an inline member function through a member function pointer,
will that function be inlined?

I imagine it wouldn't because the function that's pointed to isn't known
until run-time, is that correct?

Cheers.
Jul 23 '05 #1
8 1684
"Mat Booth" <us****@matbooth.co.uk> wrote in message
news:d1**********@newsg2.svr.pol.co.uk...
If I call an inline member function through a member function pointer,
will that function be inlined?

I imagine it wouldn't because the function that's pointed to isn't known
until run-time, is that correct?


It might be that in some cases the compiler is smart enough to know the
identity of the function whose address must be in the pointer, or that it
can do away with the pointer altogether and just inline the function. In
other cases it couldn't possibly know what the pointer contains and would
have to indirectly call a non-inlined instance of the function.

I tried this on VC++ 6.0, optimized for speed:

int N;

inline void f()
{
++N; // do some work or no code is generated
}

int main()
{
f();
void (*pf)() = &f;
(*pf)();
}

The generated code was:

inc dword ptr [N]
jmp f
f:
inc dword ptr [N]
ret

I don't know why it bothered with the redundant jump, but the compiler
clearly saw that no call was needed. In principle, a pointer to an inlined
class member function is no different, though this compiler wasn't smart
enough to generate such small code when I tried the member-function
equivalent. It ignored the pointer and called the non-inlined function
directly.

DW
Jul 23 '05 #2
test mail
plz ignore

"Mat Booth" <us****@matbooth.co.uk> wrote in message
news:d1**********@newsg2.svr.pol.co.uk...
If I call an inline member function through a member function pointer,
will that function be inlined?

I imagine it wouldn't because the function that's pointed to isn't known
until run-time, is that correct?

Cheers.

Jul 23 '05 #3
invincible schrieb:
test mail
Not a mail, but an article.
plz ignore
For this kind of posting, there is alt.test and similar groups.
comp.lang.c++ is not one of them.

"Mat Booth" <us****@matbooth.co.uk> wrote in message
news:d1**********@newsg2.svr.pol.co.uk...


Don't reply to an article unless you really reply to it.

Cheers,
Malte
Jul 23 '05 #4

"Mat Booth" <us****@matbooth.co.uk> wrote in message
news:d1**********@newsg2.svr.pol.co.uk...
If I call an inline member function through a member function pointer,
will that function be inlined?

I imagine it wouldn't because the function that's pointed to isn't known
until run-time, is that correct?


Any function which has its address taken during the course of the program
will never be inlined.

- JFA1
Jul 23 '05 #5

"James Aguilar" <jf**@cec.wustl.edu> wrote in message
news:d1**********@newsreader.wustl.edu...

"Mat Booth" <us****@matbooth.co.uk> wrote in message
news:d1**********@newsg2.svr.pol.co.uk...
If I call an inline member function through a member function pointer,
will that function be inlined?

I imagine it wouldn't because the function that's pointed to isn't known
until run-time, is that correct?


Any function which has its address taken during the course of the program
will never be inlined.

- JFA1


Never? I don't think that's correct. If I recall, the implementation is
free to inline those calls which it is able to inline. But it must also
provide a non-inline version which can be called normally. It's not an
all-or-nothing thing, but is handled on a case-by-case basis. At least,
that's my understanding.

-Howard

Jul 23 '05 #6
"Howard" <al*****@hotmail.com> wrote in message
news:Yz*********************@bgtnsc04-news.ops.worldnet.att.net...
Never? I don't think that's correct. If I recall, the implementation is free
to inline those calls which it is able to inline. But it must also provide a
non-inline version which can be called normally. It's not an all-or-nothing
thing, but is handled on a case-by-case basis. At least, that's my
understanding.


I've been wrong before, but I think I read something about how if you take the
address of a particular function, that function won't be inlined elsewhere. A
test shows . . .

--- CODE ---
inline
void inlineTest(int &i) {
i += 5;
}

int main()
{
int i = 2;
void (*testPtr)(int &) = &inlineTest;
testPtr(i);

inlineTest(i);
return 0;
}

--- CODE ---

This compiled with -S and on g++ gives this assembly code:

--- CODE ---
movl $2, -4(%ebp)
movl $__Z10inlineTestRi, -8(%ebp)
leal -4(%ebp), %eax
movl %eax, (%esp)
movl -8(%ebp), %eax
call *%eax
leal -4(%ebp), %eax
movl %eax, (%esp)
call __Z10inlineTestRi ;LINK 1
movl $0, %eax
--- CODE ---

I think that if it worked as you say, LINK 1 would be addl $5, %eax or something
like that. On the other hand, when I get the assembly compiling with -O2, crazy
stuff happens and I can't read it.

- JFA1
Jul 23 '05 #7
James Aguilar wrote:
"Howard" <al*****@hotmail.com> wrote in message
news:Yz*********************@bgtnsc04-news.ops.worldnet.att.net...
Never? I don't think that's correct. If I recall, the implementation is free
to inline those calls which it is able to inline. But it must also provide a
non-inline version which can be called normally. It's not an all-or-nothing
thing, but is handled on a case-by-case basis. At least, that's my
understanding.

I've been wrong before, but I think I read something about how if you take the
address of a particular function, that function won't be inlined elsewhere. A
test shows . . .

--- CODE ---

[...] --- CODE ---

I think that if it worked as you say, LINK 1 would be addl $5, %eax or something
like that. On the other hand, when I get the assembly compiling with -O2, crazy
stuff happens and I can't read it.


You're arguing different points here. Howard says what the Standard says.
You are demonstrating what you've observed with the compilers you have.
Both are correct but they don't contradict each other.

The Standard does not mandate either behaviour. It should be reasonably
apparent that if somebody takes the address of a function, the body has to
exist somewhere otherwise what would the address point to? However, the
act of putting a copy of the function body with all necessary adjustments
into the code instead of a call to that function (known as "inlining") is
totally up to the compiler. It doesn't have to do it even if you use the
'inline' keyword.

Between two extremes: inlining all [static] calls to that function and
also providing a body elsewhere if the address is taken *and* not inlining
the function at all, is where the behaviour of a compliant compiler lies.
You should see by now that there is nothing to argue about.

V
Jul 23 '05 #8
James Aguilar wrote:
"Howard" <al*****@hotmail.com> wrote in message
news:Yz*********************@bgtnsc04-news.ops.worldnet.att.net...
Never? I don't think that's correct. If I recall, the implementation is
free
to inline those calls which it is able to inline. But it must also
provide a
non-inline version which can be called normally. It's not an
all-or-nothing
thing, but is handled on a case-by-case basis. At least, that's my
understanding.
I've been wrong before, but I think I read something about how if you take
the address of a particular function, that function won't be inlined
elsewhere.


It's rather the other way round. If you take a pointer, a non-inline version
of the function must be created. However, that doesn't mean that the
function cannot be inlined anymore.
A test shows . . .

--- CODE ---
inline
void inlineTest(int &i) {
i += 5;
}

int main()
{
int i = 2;
void (*testPtr)(int &) = &inlineTest;
testPtr(i);

inlineTest(i);
return 0;
}

--- CODE ---

This compiled with -S and on g++ gives this assembly code:

--- CODE ---
movl $2, -4(%ebp)
movl $__Z10inlineTestRi, -8(%ebp)
leal -4(%ebp), %eax
movl %eax, (%esp)
movl -8(%ebp), %eax
call *%eax
leal -4(%ebp), %eax
movl %eax, (%esp)
call __Z10inlineTestRi ;LINK 1
movl $0, %eax
--- CODE ---

I think that if it worked as you say, LINK 1 would be addl $5, %eax or
something like that.
And where's the produced code that proves that the function gets inlined if
you remove the pointer?
On the other hand, when I get the assembly compiling with -O2, crazy stuff
happens and I can't read it.


Yes. One thing it does with -O2 is enable inlining.

Jul 23 '05 #9

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

Similar topics

6
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good...
2
by: Balbir Singh | last post by:
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...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
4
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
12
by: Tomás | last post by:
The common persuasion is: Big function -- leave it outline. Small function -- make it inline. In code I'm writing at the moment, I have a fairly long function, so it wouldn't cross my...
15
by: Lloyd Dupont | last post by:
I have some code which looks like that: public CornerStyle RectCornerMode { get { return this.GetValue<CornerStyle>(); } set { this.SetValue<CornerStyle>(value); } }
8
by: Yakov | last post by:
I'd like a tool that performed inlining of function bodies of which do not appear in the .h file. Really. gcc on Linux. Yakov
7
by: Protoman | last post by:
Is this an efficient way to integrate an explicit function of x from a to b: #include <iostream> using namespace std; class Integrate { public: Integrate(long double (*f)(long double&...
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.