virtual inline

  • Follow


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?
0
Reply clqrq (66) 11/14/2008 5:50:25 PM

..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
0
Reply v.Abazarov (13255) 11/14/2008 5:57:43 PM


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)

0
Reply pete2666 (1733) 11/14/2008 5:58:15 PM

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
0
Reply news.5.maazl2 (236) 11/15/2008 9:49:13 AM

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)

0
Reply pete2666 (1733) 11/15/2008 12:58:34 PM

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-inlining?hl=en

--
Daya
0
Reply mail.dsp (14) 11/17/2008 8:25:41 AM

5 Replies
30 Views

(page loaded in 0.131 seconds)

Similiar Articles:













7/27/2012 9:41:15 PM


Reply: