473,324 Members | 2,246 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,324 software developers and data experts.

What gets inlined?

I know this is, to some extent, implementation dependent, but since the
Standard specifies an inline specifier, there must be some "reasonable
assumptions" I can make about what should happen when I inline a function.

Suppose I have something like this (which, BTW is currently broken in
Qt-4-rc1):

inline QWidget* vBox(QWidget* parent) {
QWidget* box = new QWidget(parent);
QVBoxLayout* boxLayout = new QVBoxLayout(box);
box->setLayout(boxLayout);
return box;
}

Clearly, there will be a lot happening between the entry point, and the
return from the function. My understanding is that all of the object code
involved will not be inlined, and only the compiled representation of the
function calls will be placed inline. Is this correct?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #1
15 1449
* Steven T. Hatton:
I know this is, to some extent, implementation dependent, but since the
Standard specifies an inline specifier, there must be some "reasonable
assumptions" I can make about what should happen when I inline a function.

Suppose I have something like this (which, BTW is currently broken in
Qt-4-rc1):

inline QWidget* vBox(QWidget* parent) {
QWidget* box = new QWidget(parent);
QVBoxLayout* boxLayout = new QVBoxLayout(box);
box->setLayout(boxLayout);
return box;
}

Clearly, there will be a lot happening between the entry point, and the
return from the function. My understanding is that all of the object code
involved will not be inlined, and only the compiled representation of the
function calls will be placed inline. Is this correct?


No. Essentially you can't assume anything. And that's a FAQ:

<url: http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.3>

If you want optimization, tell your compiler to optimize.

If you want control over inlining, which is something else, use
compiler-specific language extensions.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Steven T. Hatton wrote:
I know this is, to some extent, implementation dependent, but since the
Standard specifies an inline specifier, there must be some "reasonable
assumptions" I can make about what should happen when I inline a function.


Yes. You can expect to be able to define it in multiple translation units
without getting an error. However, you can't assume any inlining to happen
or not happen.

Jul 23 '05 #3
Rolf Magnus wrote:
Steven T. Hatton wrote:
I know this is, to some extent, implementation dependent, but since the
Standard specifies an inline specifier, there must be some "reasonable
assumptions" I can make about what should happen when I inline a
function.


Yes. You can expect to be able to define it in multiple translation units
without getting an error. However, you can't assume any inlining to happen
or not happen.


My question has to do with _what_ gets inlined _if_ the compiler inlines the
function? I suspect that doesn't vary a lot between implementations, but I
could be wrong.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #4
Everywhere that the inlined function is visible the function call gets
replaced by the code in the function with appropiate substitutions for
return value and the passed variable(s).

Jul 23 '05 #5
Keep forgetting that Google quick reply chucks out the reply to text so
here again:
My question has to do with _what_ gets inlined _if_ the compiler inlines the
function? I suspect that doesn't vary a lot between implementations, but I
could be wrong.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell


If inline occurs the following:
Everywhere that the inlined function is visible the function call gets
replaced by the code in the function with appropiate substitutions for
return value and the passed variable(s).

Jul 23 '05 #6
ve*********@hotmail.com wrote:
Everywhere that the inlined function is visible the function call gets
replaced by the code in the function with appropiate substitutions for
return value and the passed variable(s).


What happens when the inlined function has calls to other functions? I
believe it is correct to assume that the object code for these functions
is /not/ inlined. Unless of course those functions are also declared
inline, and the compile feels like doing it.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #7
ve*********@hotmail.com schreef:
Everywhere that the inlined function is visible ( and possibly other places as well ) the function call gets replaced ( it the compiler follows your hint) by the code in the function ( or parts of it, e.g. if the compiler can't inline everything) with appropiate substitutions for return value and the passed
variable(s).


It's very much a hint to the compiler. What it does with such a hint
is intentionally obscure. E.g. I'd expect a very good compiler not to
inline a catch(...) clause. That's typically cold code, keep that in
a separate function but do inline the try{ } body.

HTH,
Michiel Salters

Jul 23 '05 #8

"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:Ot********************@speakeasy.net...
ve*********@hotmail.com wrote:
Everywhere that the inlined function is visible the function call gets
replaced by the code in the function with appropiate substitutions for
return value and the passed variable(s).


What happens when the inlined function has calls to other functions? I
believe it is correct to assume that the object code for these functions
is /not/ inlined. Unless of course those functions are also declared
inline, and the compile feels like doing it.


Whether the compiler will inline & substitute also subsequent function calls
in inlined functions is at the mercy of your compiler. There is no general
rule for this.

Cheers
Chris
Jul 23 '05 #9
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:f7********************@speakeasy.net
I know this is, to some extent, implementation dependent, but since
the Standard specifies an inline specifier, there must be some
"reasonable assumptions" I can make about what should happen when I
inline a function.

Suppose I have something like this (which, BTW is currently broken in
Qt-4-rc1):

inline QWidget* vBox(QWidget* parent) {
QWidget* box = new QWidget(parent);
QVBoxLayout* boxLayout = new QVBoxLayout(box);
box->setLayout(boxLayout);
return box;
}

Clearly, there will be a lot happening between the entry point, and
the return from the function. My understanding is that all of the
object code involved will not be inlined, and only the compiled
representation of the function calls will be placed inline. Is this
correct? --

Generally, yes, unless the functions called are themselves inline functions.

Lots of people in these threads talk about it being "all up to the compiler"
to inline or not inline as it sees fit. With VC++, there is a switch that
turns on "auto-inlining", whereby the compiler can inline any function it
wants. With the default settings, however, no function is inlined unless the
inline keyword is used (or the function definition is in the class
declaration, since this is equivalent to using the inline keyword), i.e.,
use of the inline keyword is a necessary, though not sufficient, condition
for a function to be inlined. For other compilers, I guess you have to
consult their documentation.

--
John Carson

Jul 23 '05 #10
* John Carson:

Lots of people in these threads talk about it being "all up to the compiler"
to inline or not inline as it sees fit. With VC++, there is a switch that
turns on "auto-inlining", whereby the compiler can inline any function it
wants. With the default settings, however, no function is inlined unless the
inline keyword is used (or the function definition is in the class
declaration, since this is equivalent to using the inline keyword), i.e.,
use of the inline keyword is a necessary, though not sufficient, condition
for a function to be inlined.
Sorry, the above is incorrect, at least for MSVC 7.1.

With MSVC 7.1 (to be precise, which is what I just checked out using a
little test program) there is a switch that turns _off_ the automatic
inlining behavior. I.e., with that switch the word 'inline' is required.
And AFAICS there's no switch to _on_ automatic inlining, it's the default.

In the for-dummies Visual Studio IDE the automatic inlining off switch is
set by default, and, just to give readers who don't have that IDE an
impression of how intelligent the designers were, switches that make the
compiler behave as standard C++ wrt. wchar_t type, 'main', RTTI and more are
not set by default in the IDE -- it's not a Good Idea to use the compiler
behavior in a default Microsoft IDE project as any indication of anything.

For other compilers, I guess you have to consult their documentation.


That would also be a good idea for MSVC. ;-)

/Ob0 Disables inline expansion, which is on by default.

Not taking the documentation's word for it, because it's often wrong, I
tested using a simple program and checking the generated machine code.

Advice: always test reality.
Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #11
"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net
* John Carson:

Lots of people in these threads talk about it being "all up to the
compiler" to inline or not inline as it sees fit. With VC++, there
is a switch that turns on "auto-inlining", whereby the compiler can
inline any function it wants. With the default settings, however, no
function is inlined unless the inline keyword is used (or the
function definition is in the class declaration, since this is
equivalent to using the inline keyword), i.e., use of the inline
keyword is a necessary, though not sufficient, condition for a
function to be inlined.


Sorry, the above is incorrect, at least for MSVC 7.1.

With MSVC 7.1 (to be precise, which is what I just checked out using a
little test program) there is a switch that turns _off_ the automatic
inlining behavior. I.e., with that switch the word 'inline' is
required. And AFAICS there's no switch to _on_ automatic inlining,
it's the default.


It is controlled by the /Ob switch. /Ob0 turn off inlining, as you state,
/Ob1 gives manual inlining and /Ob2 gives auto inlining. If you create a
project in the IDE in VC++ 7.1, then /Ob2 (automatic) is the default in
Release mode. I stand corrected on this. It is a change from VC++ 6.0, where
/Ob1 is the IDE default. The change in defaults was made in VC++ 7.0.
--
John Carson

Jul 23 '05 #12
msalters wrote:
ve*********@hotmail.com schreef:
Everywhere that the inlined function is visible

( and possibly other places as well )


Could you please explain what you mean by this? And how does the
compiler/linker figure out what code to use if it can't see the code?
the function call gets replaced

( it the compiler follows your hint)


Just responded to the lets assume the compiler does follow your hint :)
by the code in the function

( or parts of it, e.g. if the compiler can't inline everything)


What compiler(s) implement this at the moment? For the few I know it's
an all or nothing afair.
with appropiate substitutions for return value and the passed
variable(s).


It's very much a hint to the compiler. What it does with such a hint
is intentionally obscure. E.g. I'd expect a very good compiler not to
inline a catch(...) clause. That's typically cold code, keep that in
a separate function but do inline the try{ } body.

HTH,
Michiel Salters


Jul 23 '05 #13
Steven T. Hatton wrote:

Suppose I have something like this (which, BTW is currently broken
in Qt-4-rc1):

inline QWidget* vBox(QWidget* parent) {
QWidget* box = new QWidget(parent);
QVBoxLayout* boxLayout = new QVBoxLayout(box);
box->setLayout(boxLayout);
return box;
}


This code is highly non-exception-safe. Is that what you meant
by 'broken' ? If the 'new QVBoxLayout(box)' call fails (either
due to lack of memory, or the layout throwing an exception),
then resources will be leaked.
I can't see how a project that's supposed to be the basis of
a stable application, can justify such dreadful code ?

Jul 23 '05 #14
Old Wolf wrote:
Steven T. Hatton wrote:

Suppose I have something like this (which, BTW is currently broken
in Qt-4-rc1):

inline QWidget* vBox(QWidget* parent) {
QWidget* box = new QWidget(parent);
QVBoxLayout* boxLayout = new QVBoxLayout(box);
box->setLayout(boxLayout);
return box;
}
This code is highly non-exception-safe. Is that what you meant
by 'broken' ?


Probably not, considering that Qt doesn't use exceptions.
If the 'new QVBoxLayout(box)' call fails (either
due to lack of memory, or the layout throwing an exception),
then resources will be leaked.
Probably the exception will actually not be caught at all an the process be
terminated. There isn't much that the program could do anyway.
I can't see how a project that's supposed to be the basis of
a stable application, can justify such dreadful code ?


If there is no memory left, the system is likely to be next to unresponsive
due to excessive swapping. And then, the program cannot do much to react to
an out-of-memory condition. Opening a window to tell that to the user - or
even creating a string would require more memory and so cannot be done in
an exception handler for out-of-memory exceptions.

Jul 23 '05 #15
Old Wolf wrote:
Steven T. Hatton wrote:

Suppose I have something like this (which, BTW is currently broken
in Qt-4-rc1):

inline QWidget* vBox(QWidget* parent) {
QWidget* box = new QWidget(parent);
QVBoxLayout* boxLayout = new QVBoxLayout(box);
box->setLayout(boxLayout);
return box;
}


This code is highly non-exception-safe. Is that what you meant
by 'broken' ? If the 'new QVBoxLayout(box)' call fails (either
due to lack of memory, or the layout throwing an exception),
then resources will be leaked.
I can't see how a project that's supposed to be the basis of
a stable application, can justify such dreadful code ?


Qt cleans up after itself. If QVBoxLayout(box) fails, parent still has a
handle on box, and will destroy box when it's destroyed. As it turns out,
Qt does not support the functionality I was trying to use. The
documentation is out of sync with the current state of the development code
base. I believe they are dumbing down the widget and layout creation
features because too many people found it confusing. Unfortunately, I took
the time, and made the effort to figure it out. Once I understood it, I
was able to do some really slick stuff with it. Now that they're taking it
out, all my code is deprecated. :(
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
21
by: Mahesh Devjibhai Dhola [MVP] | last post by:
Hi, I have added few of the events in some control, example code is: btnControl.GotFocus +=new EventHandler(EventHandlingMethod); btnControl.Click +=new EventHandler(EventHandlingMethod);...
4
by: sks | last post by:
All, I thought all template instantiations were inlined until I analyzed the following code. template<typename t> class Something { public: inline int getSomething() { return 1; }
2
by: Michael Nemtsev | last post by:
AFAIK, method could be candidate for inlining. But is it possible to view after compiling that method became inlined? Are there some keywords in ILDASM that can show that method is inlined? ---...
9
by: Bilgehan.Balban | last post by:
Hi, If I define an inline function in one .c file, and use it from another, after compiling and linking the two, it seems the function is not inlined but rather called as a regular function. I...
2
by: newbie | last post by:
I happened to read boost library code and realized that most (the part I read) functions are inlined like: template <class Key> inline void Foo(const Key& k) { ... ... } Is there a strong...
4
by: Zytan | last post by:
Can I force a function to NEVER be inlined? I want the call stack returned by: System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace(); to be such that:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.