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

virtual inline

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
5 2528
..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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
15
by: Dave Townsend | last post by:
Yo, I had a job interview today, the interviewing asked me about inline virtual functions, or what was my opinion on them. Hm, I've seen mention of these babies in the reference material, but...
9
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
3
by: shuisheng | last post by:
Dear All, If I define a virtual function to be inline, is it really inline? Or it is inline in some cases, and not in other cases. Would you please help me to look at the following case. ...
11
by: Chris Thomasson | last post by:
Consider an an object that that can has 7 or 8 functions. If you create an abstract base class for the "interface" of the object, well, that means 7 or 8 pure virtual functions right? Well, IMHO,...
8
by: siddhu | last post by:
Dear experts, A virtual function has to have an address. So if an inline virtual function is actually inlined then in that case what does address of this function signify? How does compiler know...
5
by: sunil | last post by:
Hello, I have a class deriving from a class that provides ability to serialize/deserialize objects over the network. There are two classes Requests (sent from client to server) Response(sent from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.