[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

virtual inline

.rhavin grobert

11/14/2008 5:50:00 PM

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?
5 Answers

Victor Bazarov

11/14/2008 5:58:00 PM

0

..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

Pete Becker

11/14/2008 5:58:00 PM

0

On 2008-11-14 12:50:25 -0500, ".rhavin grobert" <clqrq@yahoo.de> said:

> 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)

Marcel Müller

11/15/2008 9:49:00 AM

0

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

Pete Becker

11/15/2008 12:59:00 PM

0

On 2008-11-15 04:49:13 -0500, Marcel Müller
<news.5.maazl@spamgourmet.com> said:

>
> 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)

mail.dsp

11/17/2008 8:26:00 AM

0

On Nov 14, 10:50 pm, ".rhavin grobert" <cl...@yahoo.de> 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? is foo always (jumped /
> called / ...) (<-?) for Derived?

For more information visit this link:
http://groups.google.com/group/CPP-newsgroup/web/standard-c-programming-virtual-functions-and-inlining-virtual-functions-and-inli...

--
Daya