J
John Ratliff
Can the compiler ever inline a method when there is a pointer to the
member used?
Thanks,
--John Ratliff
member used?
Thanks,
--John Ratliff
Can the compiler ever inline a method when there is a pointer to the
member used?
John said:Can the compiler ever inline a method when there is a pointer to the
member used?
John said:Can the compiler ever inline a method when there is a pointer to the
member used?
Alf said:* John Ratliff:
Do you mean, "when there is a pointer to the method used", and by "method" you
mean non-static member function?
If so, yes, it can.
Or, depending on what you mean by "compiler", and how tied to 2005 technology
you want the answer, with e.g. MSVC it's the linker that does this job.
Greg said:If the question is: does taking the address of a function (or a method)
prevent the compiler from inlining that function, the answer is "no".
Remember that function calls - not functions themselves - are inlined.
Whether the compiler decides to inline a particular function call has
no relation to other code that may take the address of the function
whose call is being inlined.
If the question is: can a function call through a function (or member)
pointer be inlined? The answer is "no" for any sensibly-written
program. To inline a function call through a pointer, the compiler
would have to be certain that the pointer would always point to the
same function on every call. And the compiler would have to be able to
deduce which function the pointer points to. But if the function
pointer never varies, why would the program bother with it and not just
call the pointed-to function directly? Use of a function pointer only
makes sense when the pointer has more than one function it could be
pointing to.
John said:This is what I was looking to understand. Thanks.
This was my problem. How could it inline a call through a pointer to member?
I would first determine whether inlining the call through the member
pointer would really have a measurable effect on the program's
performance. In all likelihood, it would not. Performance problems that
can be solved with inlining alone are few and far between in my
experience.
Note also that if the method being called is virtual, the likelihood
that the compiler would inline even a direct call to the method are
also low. Inlining a call to a virtual function is possible only in
limited cases, when the type of object whose method is being invoked is
both known and unvarying.
Using a function object instead of a function pointer often provides
better inlining potential, since the type of the function object is
usually known at compile time. Replacing the member function pointer
with an enum that is used as a selector in a switch statement to
dispatch the function call would be another way to make the call more
suitable for inlining.
...
A good IDE would probably be just as good of a solution,
but until I find one....
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.