virtual obliterates nested inline?

E

er

hi,

does virtual obliterates nested inline?

in the example below, does it make any difference to D::f() that B's
member functions are inlined? does the answer depend on whether i use
D.f() or A*->f()?

class B{
/ * inline member functions */
};//concrete class

class A{
public:
virtual void f()=0;
}

class D: public A{
public:
virtual void f();// uses b
private:
B b;
}
 
G

Guest

hi,

does virtual obliterates nested inline?

in the example below, does it make any difference to D::f() that B's
member functions are inlined? does the answer depend on whether i use
D.f() or A*->f()?

If a function is virtual or not affects the way that function is called,
not what it does when it executed. So whether B's functions are inlined
or not have nothing to do with f() being virtual or not.
 
E

er

If a function is virtual or not affects the way that function is called,
not what it does when it executed. So whether B's functions are inlined
or not have nothing to do with f() being virtual or not.

ok, thanks. so i take it i should go ahead an inline within B if want
speed.
 
Z

Zilla

ok, thanks. so i take it i should go ahead an inline within B if want
speed.

"virtual" tells the compiler to add a function in a class virtual
table; "inline" tells the compiler to substitute function call with
inline code, so

inline void D::foo()
{
b+1;
}

d=foo();

gets substituted, during compile time, with

d=b+1;
 
G

Guest

"virtual" tells the compiler to add a function in a class virtual
table; "inline" tells the compiler to substitute function call with
inline code, so

Actually it only suggests that the compiler may substitute the call with
the function body, the compiler does not have to do so. I also think
that the compiler may substitute function calls even for functions not
marked as inline. So one should not assume that code becomes faster just
by inlining some functions, and one have to be aware of the other
consequences. For more information please read the FAQ about inline:
http://www.coders2020.com/cplusplus-explained/inline-functions.html
 
E

er

Actually it only suggests that the compiler may substitute the call with
the function body, the compiler does not have to do so. I also think
that the compiler may substitute function calls even for functions not
marked as inline. So one should not assume that code becomes faster just
by inlining some functions, and one have to be aware of the other
consequences. For more information please read the FAQ about inline:http://www.coders2020.com/cplusplus-explained/inline-functions.html

thanks again. as much as i appreciate your word of caution, i hope you
won't mind it if i ask a final clarification: let's say that every
member function in B meets is a good candidate for inlining, for
example an accessor B::func()const{return x;}. if the only purpose of
B is to be used by a virtual function of D (derived from A). is it
worth it or not to inline B::func()const?
 
Z

Zilla

Actually it only suggests that the compiler may substitute the call with
the function body, the compiler does not have to do so. I also think
that the compiler may substitute function calls even for functions not
marked as inline. So one should not assume that code becomes faster just
by inlining some functions, and one have to be aware of the other
consequences. For more information please read the FAQ about inline:http://www.coders2020.com/cplusplus-explained/inline-functions.html

I always (and maybe exclusively) use inline functions for query const
functions, like

inline bool MyClass::trueOrFalse() const
{
return _trueOrFalse;
}

The speed improvement comes from not having to make a function call;
inlining multiple line functions causes bloat (as the posted FAQ
suggests), slowing real-time performance.
 
K

Kai-Uwe Bux

er said:
thanks again. as much as i appreciate your word of caution, i hope you
won't mind it if i ask a final clarification: let's say that every
member function in B meets is a good candidate for inlining, for
example an accessor B::func()const{return x;}. if the only purpose of
B is to be used by a virtual function of D (derived from A). is it
worth it or not to inline B::func()const?

You need to measure. How inline affects speed cannot be answered in the
abstract. Technically, the keyword inline is just a promise that all
definitions of the function as found in various translation units will
agree (and hence obey the one definition rule). Based on that promise, the
compiler may or may not perform certain optimizations, which the compiler
is allowed to perform anyway and which may or may not pay off.

When it comes to performance tuning, you always need to profile and act upon
your measurements.


Best

Kai-Uwe Bux
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top