inline functions

N

niklaus

Hi,
I have a doubt regarding inline functions.

1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.

2) Is it the source code that is inlined or the assembly code (object
code).

3) If i have a function like this

inline int add(int a,int b)
{
return a++ + ++b;

}

int main()
{
int k ,m,n;
m=3;
n=4;
k= add(m,n);
}

does something like this happen ? * i am not sure about this *
int main()
{
int k ;
m=3;
n=4;
tmp1=m;
tmp2=n;
k=( tmp1++ + ++tmp2);
}

How do i see something like this (i mean expanded version of inline
function ).
 
V

Victor Bazarov

1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.

In between.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.

No, inlining is not macro substitution. It's done during code generation
so you should be able to see the results in the assembly listing if your
compiler is capable of producing one.
2) Is it the source code that is inlined or the assembly code (object
code).

Machine code.
3) If i have a function like this

inline int add(int a,int b)
{
return a++ + ++b;

}

int main()
{
int k ,m,n;
m=3;
n=4;
k= add(m,n);
}

does something like this happen ? * i am not sure about this *
int main()
{
int k ;
m=3;
n=4;
tmp1=m;
tmp2=n;
k=( tmp1++ + ++tmp2);

Something like this, yes.
}

How do i see something like this (i mean expanded version of inline
function ).

You need to look at the assembly listing.

V
 
P

Phlip

niklaus said:
I have a doubt regarding inline functions.

1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.

'inline' does not mean opcodes are inserted in-line with their calling
functions.

It only means that a compiler may see the inline function's body many times
in each "translation unit". Otherwise, the compiler must only see one body
for each function. (This is why template functions and class member
functions defined inside the function get an automatic 'inline'.)

Once the compiler knows all the functions, it is free to inline and
out-of-line them as it likes, based on its optimization system. Some
out-of-line functions might be inlined, and some inline functions might be
out-of-lined. The compiler can do anything it likes so long as the resulting
opcodes' behaviors are indistinguishable from C++'s language definition.

You should _never_ make something inline just because you guess it must be
faster. Google "premature optimization". Some teams write everything
out-of-line, as a rule, until profiling reveals something should be inline.
However, you will still get better performance by profiling and changing
entire algorithms, not just by tweaking small details.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.

Inline are not macros, which are a text substitution mechanism added
literally on top of the C++ language.

You may disassemble the output, and whether you get inlines where you expect
may even depend on your compiler settings.
2) Is it the source code that is inlined or the assembly code (object
code).

Object code.
3) If i have a function like this

inline int add(int a,int b)
{
return a++ + ++b;

}

int main()
{
int k ,m,n;
m=3;
n=4;
k= add(m,n);
}

does something like this happen ? * i am not sure about this *
int main()
{
int k ;
m=3;
n=4;
tmp1=m;
tmp2=n;
k=( tmp1++ + ++tmp2);
}

Maybe (modulo a couple errors in your code). What's more important is you
can't tell what happened to add(). Maybe it went away, or maybe it's there.
How do i see something like this (i mean expanded version of inline
function ).

You never can. You can only disassemble your code, or run a debugger on it.
(And compiling for debugging often changes the optimizations. This is
entirely platform specific, so it's off-topic here.)

If you are curious, read a book like /Inside the C++ Object Model/ by
Lippman. However, if you are new to programming, you should instead be
researching details like /Design Patterns/ or unit testing. Those will make
you a faster programmer.

C++ programs are already very fast. An inline here and there will not make a
program faster.
 
D

David Harmon

On Tue, 29 Aug 2006 19:09:52 GMT in comp.lang.c++, "Phlip"
It only means that a compiler may see the inline function's body many times
in each "translation unit". Otherwise, the compiler must only see one body
for each function.

Well, _once_ in each translation unit; many times over the entire
program.
 
R

Rolf Magnus

Victor said:
In between.


No, inlining is not macro substitution. It's done during code generation
so you should be able to see the results in the assembly listing if your
compiler is capable of producing one.


Machine code.

It's probably somewhere in between (as you mentioned in 1), in an
intermediate compiler-internal representation. That way, the inlined code
can benefit from the optimizations that are done before code generation.
 
P

Phlip

David said:
Well, _once_ in each translation unit; many times over the entire
program.

g++ concurs; thanks.

My prose style was relaxed, but it did indeed reveal I didn't know that. I
never needed to learn it because I always follow immaculate coding styles.
 
F

Frederick Gotham

Phlip posted:
You should _never_ make something inline just because you guess it must
be faster.


Not even the following?

unsigned inline Square(unsigned const i)
{
return i*i;
}

Granted, no competant programmer would use this function, but you get the
idea.
 
A

Andreas Pagel

Frederick said:
unsigned inline Square(unsigned const i)
{
return i*i;
}

Granted, no competant programmer would use this function,

You've got me curious there - why do you say that? I've used exactly such a
function (except for making the argument a template rather than unsigned).

Andreas.
 
M

Michiel.Salters

Frederick said:
Phlip posted:


Not even the following?

unsigned inline Square(unsigned const i)
{
return i*i;
}

Granted, no competant programmer would use this function, but you get the
idea.

Indeed. It takes 7 extra characters, modern compilers can still inline
it, and there
is reason from profiling to suggest you should actually inline it.

Now, if a method ends up implicit inline (defined in class definition)
I don't care.
The added advantage of saying to maintainters "just a trivial accessor
method,
the real logic is in Foo::Bar()" is usually bigger than the performance
advantage.

Regards,
Michiel Salters
"
 
E

Earl Purple

Indeed. It takes 7 extra characters, modern compilers can still inline
it, and there
is reason from profiling to suggest you should actually inline it.

You would inline it so you can stick it in a utilities header somewhere
and include that header without having to link against something.

Of course you would probably make it a template, in which case you
would not need to use the inline keyword.
 
F

Frederick Gotham

Andreas Pagel posted:
You've got me curious there - why do you say that? I've used exactly
such a function (except for making the argument a template rather than


I rather simply write:

x*x
 
A

Andreas Pagel

Frederick said:
Andreas Pagel posted:



I rather simply write:

x*x

Ok, but sometimes you might want to square a value that isn't available in a
variable. What about Square(a++)? You'd have to write a++ * a, already it
looks a little messier. What about Square((a+b+c+d+e)/5)? Now you'd have
to repeat a long expression.

Or what about Square(fn(...))? Now you'd need to introduce a temporary
variable.

Andreas.
 
A

Andreas Pagel

I said:
What about Square(a++)? You'd have to write a++ * a, already it
looks a little messier.

Of course, not only messier, but wrong. No need for a bunch of follow-ups
pointing this out...

Andreas.
 
F

Frederick Gotham

Andreas Pagel posted:
Ok, but sometimes you might want to square a value that isn't available
in a variable. What about Square(a++)? You'd have to write a++ * a,
already it looks a little messier. What about Square((a+b+c+d+e)/5)?
Now you'd have to repeat a long expression.

Or what about Square(fn(...))? Now you'd need to introduce a temporary
variable.


Oh I see your point, I hadn't thought of that. In such cases, I'd probably
do:

template<class T>
T Square(T const a, T const b)
{
return a * b;
}

(By the way,

a++ * a

invokes undefined behaviour.)
 
A

Andreas Pagel

Frederick said:
Andreas Pagel posted:



Oh I see your point, I hadn't thought of that.

Fair enough. I was surprised by your initial comment and wondered if there
was some obscure point I'd overlooked...
In such cases, I'd probably
do:

template<class T>
T Square(T const a, T const b)
{
return a * b;
}

Except that isn't a square function any more, but a generic multiplication.
I can't see that that buys you anything over just writing "a * b".

(Regarding the undefined behaviour - I noticed my error soon after posting.)

Andreas.
 
F

Frederick Gotham

Andreas Pagel posted:
Except that isn't a square function any more, but a generic
multiplication. I can't see that that buys you anything over just
writing "a * b".


I wrote quickly and sloppily... should have written:

template<class T>
T Square(T const val)
{
return val*val;
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top