Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inline function

Reply
Thread Tools

inline function

 
 
jg
Guest
Posts: n/a
 
      07-30-2007
Does C++ standard require an inline function be generated all the
time ?

For example,

#include <iostream>
using namespace std;

inline int foo()
{
return 10;
}

int main()
{
cout << "foo()=" << foo() << endl;
}

Should foo() be generated in the final executable all the time ?

JG

 
Reply With Quote
 
 
 
 
tony_in_da_uk@yahoo.co.uk
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 8:51 am, jg <(E-Mail Removed)> wrote:
> Does C++ standard require an inline function be generated all the
> time ?


inline is just a hint or request of the compiler, and doesn't promise
anything...

Tony

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 4:51 am, jg <(E-Mail Removed)> wrote:
> Does C++ standard require an inline function be generated all the
> time ?
>

The correct term is "inlined", not "generated".

Given that, the answer is that the standard doesnot require an inline
function to be inlined all the time.

-N


 
Reply With Quote
 
jg
Guest
Posts: n/a
 
      07-31-2007
On Jul 30, 8:47 pm, Neelesh Bodas <(E-Mail Removed)> wrote:
> On Jul 31, 4:51 am, jg <(E-Mail Removed)> wrote:> Does C++ standard require an inline function be generated all the
> > time ?

>
> The correct term is "inlined", not "generated".
>
> Given that, the answer is that the standard doesnot require an inline
> function to be inlined all the time.
>
> -N


No. I mean "generated". Whether an inline function is inlined or not
is not my question. My question is whether a compiler will generate
the code for that inline function no matter whether the function
is inlined or not.

For my example, I tried g++. Without optimization, it does generate
foo(); with -O, it does not. With Sun Studio 11, it does not generate
foo() even without optimization.

JG



it does generate that

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-31-2007
jg wrote:
> On Jul 30, 8:47 pm, Neelesh Bodas <(E-Mail Removed)> wrote:
>> On Jul 31, 4:51 am, jg <(E-Mail Removed)> wrote:> Does C++ standard require an inline function be generated all the
>>> time ?

>> The correct term is "inlined", not "generated".
>>
>> Given that, the answer is that the standard doesnot require an inline
>> function to be inlined all the time.
>>
>> -N

>
> No. I mean "generated". Whether an inline function is inlined or not
> is not my question. My question is whether a compiler will generate
> the code for that inline function no matter whether the function
> is inlined or not.
>

The answer has to be yes, if the function is used. Code will either be
generated inline where it is called, or elsewhere if the compiler
decides not to inline the function.

There is no requirement to generate a stand alone function.

--
Ian Collins.
 
Reply With Quote
 
nallayan77@gmail.com
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 10:12 am, Ian Collins <(E-Mail Removed)> wrote:
> jg wrote:
> > On Jul 30, 8:47 pm, Neelesh Bodas <(E-Mail Removed)> wrote:
> >> On Jul 31, 4:51 am, jg <(E-Mail Removed)> wrote:> Does C++ standard require an inline function be generated all the
> >>> time ?
> >> The correct term is "inlined", not "generated".

>
> >> Given that, the answer is that the standard doesnot require an inline
> >> function to be inlined all the time.

>
> >> -N

>
> > No. I mean "generated". Whether an inline function is inlined or not
> > is not my question. My question is whether a compiler will generate
> > the code for that inline function no matter whether the function
> > is inlined or not.

>
> The answer has to be yes, if the function is used. Code will either be
> generated inline where it is called, or elsewhere if the compiler
> decides not to inline the function.
>
> There is no requirement to generate a stand alone function.
>
> --
> Ian Collins.


Hi Jg,
It is up to the compiler based on the size of the piece of
inline code constructed by the programmer.
If the compiler feels not to make the piece as inline(because of
larger size), it does not generate the inline code.

Thanks,
nalla

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 1:51 am, jg <(E-Mail Removed)> wrote:
> Does C++ standard require an inline function be generated all the
> time ?


> For example,


> #include <iostream>
> using namespace std;


> inline int foo()
> {
> return 10;
> }


> int main()
> {
> cout << "foo()=" << foo() << endl;
> }


> Should foo() be generated in the final executable all the time ?


I'm not sure what you mean by "generated". The language defines
the semantics of a given C++ program (provided there is no
undefined behavior). Those semantics result in one or more
possible "observable behavior". Basically, the language
standard then says that a compiler can do whatever it wants, as
long as executing the resulting program results in one of the
observable behaviors.

On a Unix, machine, for example, for the above a compiler could
legally generate something like:
write( 1, "foo()=10\n", 9 ) ;
return 0 ;
Most compilers will probably generate something like:
cout << "foo()=" << 10 << endl ;
return 0 ;
for the above.

Some compilers might also generate an out of line copy of foo(),
perhaps to simplify debugging (e.g. if you've asked for
debugging information). What the compiler generates, however,
doesn't matter as long as executing the code results in an
observable behavior which corresponds to the specified
semantics.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-31-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Jul 31, 10:12 am, Ian Collins <(E-Mail Removed)> wrote:
>> jg wrote:
>>> On Jul 30, 8:47 pm, Neelesh Bodas <(E-Mail Removed)> wrote:
>>>> On Jul 31, 4:51 am, jg <(E-Mail Removed)> wrote:> Does C++ standard require an inline function be generated all the
>>>>> time ?
>>>> The correct term is "inlined", not "generated".
>>>> Given that, the answer is that the standard doesnot require an inline
>>>> function to be inlined all the time.
>>>> -N
>>> No. I mean "generated". Whether an inline function is inlined or not
>>> is not my question. My question is whether a compiler will generate
>>> the code for that inline function no matter whether the function
>>> is inlined or not.

>> The answer has to be yes, if the function is used. Code will either be
>> generated inline where it is called, or elsewhere if the compiler
>> decides not to inline the function.
>>
>> There is no requirement to generate a stand alone function.
>>
>> --
>> Ian Collins.

>
> Hi Jg,
> It is up to the compiler based on the size of the piece of
> inline code constructed by the programmer.
> If the compiler feels not to make the piece as inline(because of
> larger size), it does not generate the inline code.


I think you are all being a bit obtuse here. He is clearly asking
whether a (non-inlined) subroutine for the function will be generated
in the final binary regardless of whether the compiler inlined its
contents in the only place where it was called or not.

Or if we word it in another way: Even if the compiler inlined the
contents of the function at the calling location, will the contents
of the function also exist as a non-inlined function in the final
binary (even if this non-inlined version is never called anywhere)?

I don't know what the standard says, but IIRC at least gcc has a
command-line option to force it to create non-inlined instances of
inline functions regardless of whether they are necessary or not.
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      07-31-2007
jg <(E-Mail Removed)> wrote in news:1185839484.488728.323430
@b79g2000hse.googlegroups.com:

> Does C++ standard require an inline function be generated all the
> time ?
>
> For example,
>
> #include <iostream>
> using namespace std;
>
> inline int foo()
> {
> return 10;
> }
>
> int main()
> {
> cout << "foo()=" << foo() << endl;
> }
>
> Should foo() be generated in the final executable all the time ?


Not necessarily. If the function is declared as inline, the function body
may never exist in the final executable. There are certain actions which
will guarantee that it does exist (like taking it's address), and you can't
guarantee that the function body will _not_ be in the executable.
 
Reply With Quote
 
jg
Guest
Posts: n/a
 
      08-01-2007
Thanks for all answers.

To summarize, C++ standard does not require that the out-of-line code
of an inline function be generated all the time. It looks like C++
standard does not have explicit wording about this (at least I don't
find one); and I take it as whether to generate out-of-line code is a
up to an implementation.

The reason I was asking this is that in C Standard (C99), a compiler
must not generate the out-of-line code of an inline function, even
there
are calls to that inline function and those calls are not inlined.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to know the inline-function was implemented the inline way or normal way? ypjofficial@indiatimes.com C++ 7 07-18-2006 11:22 PM
forcing compiler to consider inline function inline. Ajay C++ 5 04-01-2006 02:03 PM
Use of inline function in inline function - is it allowed? TGOS C Programming 3 02-28-2005 10:49 AM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments