By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

virtual inline

P: n/a
if you have

class Base {
virtual inline bool foo() {return false;};
};

class Derived: public Base {
virtual bool foo();
};
bool Derived:foo()
{
/* lot's of code here */
return true;
};

is foo() always inlined for Base? is foo always (jumped /
called / ...) (<-?) for Derived?
Nov 14 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
..rhavin grobert wrote:
if you have

class Base {
virtual inline bool foo() {return false;};
};

class Derived: public Base {
virtual bool foo();
};
bool Derived:foo()
{
/* lot's of code here */
return true;
};

is foo() always inlined for Base?
What does that mean?
is foo always (jumped /
called / ...) (<-?) for Derived?
What does that mean?

A function cannot be "always inlined" or "never inlined". You have no
control over that - the compiler is free to do what it thinks is best,
neither do you have any way of knowing what the compiler did - there is
no portable way to determine if a particular function was ever inlined
or not. Why do you care?

The concepts of dynamic binding (virtual functions) and inlining are
orthogonal. If the compiler knows that it needs to call 'Base::foo' and
it has the implementation handy (and it's short like that), it will
*probably* (or, perhaps, *hopefully*) inline it. If the compiler needs
to involve run-time resolution (i.e. it has a pointer or a reference to
'Base' and calls 'foo' with that), it will generate code to call the
function in a particular way (v-table, etc.) and "inlining" has nothing
to do with that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 14 '08 #2

P: n/a
On 2008-11-14 12:50:25 -0500, ".rhavin grobert" <cl***@yahoo.desaid:
if you have

class Base {
virtual inline bool foo() {return false;};
};

class Derived: public Base {
virtual bool foo();
};
bool Derived:foo()
{
/* lot's of code here */
return true;
};

is foo() always inlined for Base? is foo always (jumped /
called / ...) (<-?) for Derived?
Ask your compiler. There's no requirement that inline functions be
expaned inline.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 14 '08 #3

P: n/a
Hi!

..rhavin grobert schrieb:
class Base {
virtual inline bool foo() {return false;};
};

class Derived: public Base {
virtual bool foo();
};

bool Derived:foo()
{
/* lot's of code here */
return true;
};

is foo() always inlined for Base? is foo always (jumped /
called / ...) (<-?) for Derived?
From your code Base::foo is never called at all, so you cannot answer
the question whether it is expanded inline or not.

Derived::foo cannot be expanded inline unless your compiler uses a two
pass method to examine the body of the function.

Furthermore, a virtual function may not be expanded inline unless the
compiler knows for sure, that it cannot be overloaded. The reason is
simply that the required function is determined at runtime and may not
even be written at the time when the compiler generates the call.
Only if the type is known for sure, the run time dispatch can be
optimized. E.g.
Base b;
b.foo();
may expand Base::foo inline.
But as soon as b becomes a reference or pointer type, a run time
dispatch is required.
(Languages with a 'final' attribute for functions and classes have a
significantly higher probability of the above optimization.)
Marcel
Nov 15 '08 #4

P: n/a
On 2008-11-15 04:49:13 -0500, Marcel Müller
<ne**********@spamgourmet.comsaid:
>
Furthermore, a virtual function may not be expanded inline unless the
compiler knows for sure, that it cannot be overloaded.
Overloading has nothing to do with it.
The reason is simply that the required function is determined at
runtime and may not even be written at the time when the compiler
generates the call.
This is not overloading. It is overriding.
Only if the type is known for sure, the run time dispatch can be
optimized. E.g.
Base b;
b.foo();
may expand Base::foo inline.
But as soon as b becomes a reference or pointer type, a run time
dispatch is required.
No, not required. Just quite common. But if the compiler can determine
the actual type of the object it can expand the function inline.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 15 '08 #5

P: n/a
On Nov 14, 10:50 pm, ".rhavin grobert" <cl...@yahoo.dewrote:
if you have

class Base {
virtual inline bool foo() {return false;};

};

class Derived: public Base {
virtual bool foo();

};

bool Derived:foo()
{
/* lot's of code here */
return true;

};

is foo() always inlined for Base? is foo always (jumped /
called / ...) (<-?) for Derived?
For more information visit this link:
http://groups.google.com/group/CPP-n...inlining?hl=en

--
Daya
Nov 17 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.