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

Question about Inline functions.

44
Hi,

This is abhinay,
I have following question about inline function.

Thanks in advance.

1. In case of Inline function, function call is replace by its code defination, then
if any inline function have return statement and if it is called by main() then it is suppose to part of main, then why program is not terminated. ?

ex:
Expand|Select|Wrap|Line Numbers
  1. class Test
  2. {
  3.  
  4. public:
  5.     Test(){}
  6.     inline long print();
  7.     inline void afterPrint();
  8. };
  9.  
  10. long inline Test::print()
  11. {
  12.     cout<<"In side Print "<<endl;
  13.     return lNo;
  14. }
  15.  
  16. void inline Test::afterPrint()
  17. {
  18.     cout<<"In side afterPrint "<<endl;
  19.  
  20. }
  21.  
  22. void main()
  23. {
  24.     Test test;
  25.     test.print();
  26.     test.afterPrint();
  27. }
  28.  
Output:

In side Print
In side afterPrint

Regards
Abhinay
May 8 '07 #1
8 2182
svlsr2000
181 Expert 100+
Good logical thinking. :)

Inline is just request to compiler, its not guaranteed to be replaced.
More ever "Exact text replacement occurs only in macros, not in inline functions".

The compiler replaces in such a way that main doesnt exit
May 8 '07 #2
ajayraj
21
The code replacement means not the code as such, but while on creating the object file from the Cpp file( i.e on external Linking ), it replaces code.

It is not like the macro and it will act like a function call itself and if u debug the internal assembly ( Then u can see that the code print() is replaced with a few lines of intructions that too inside the main() call ) then things will be clear.

So if u specify a long variable then you can store the returned value in that variable from the function "print()".
Its possible just because it acts to be ordinary function, but with a change in the external linkage.

This is the reason that it executed the next intructions after a inline call.
Hoe u got it clear.
May 8 '07 #3
AdrianH
1,251 Expert 1GB
Hi,

This is abhinay,
I have following question about inline function.

Thanks in advance.

1. In case of Inline function, function call is replace by its code defination, then
if any inline function have return statement and if it is called by main() then it is suppose to part of main, then why program is not terminated. ?
I do not understand your question. What do you mean by "... then why program is not terminated. ?"


Adrian
May 8 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
1. In case of Inline function, function call is replace by its code defination, then
if any inline function have return statement and if it is called by main() then it is suppose to part of main, then why program is not terminated. ?
An inline function is not a call. At the place you call the inline function in
main(), the code for the function is copied inline. For example:
Expand|Select|Wrap|Line Numbers
  1. int inline funct()
  2. {
  3.     return 5;
  4. }
  5.  
  6. int main()
  7. {
  8.     int x = funct();
  9.  
  10. }
  11.  
will be compiled as:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int x = 5;
  4.  
  5. }
  6.  
so after the return of funct() you are still in main(). That is why your prgram is not terminated.
May 8 '07 #5
Also remember that an inline function is a request to the compiler, so even though your function is written as an inline it may not end up that way in the executable.
May 8 '07 #6
AdrianH
1,251 Expert 1GB
Also remember that an inline function is a request to the compiler, so even though your function is written as an inline it may not end up that way in the executable.
Yes, very true. But it will not cause an error.


Adrian
May 8 '07 #7
ajayraj
21
Yes, very true. But it will not cause an error.


Adrian
But also you can force ur compiler to make a function inline
using
__forceinline

example
long __forceinline function()
{
cout<<"hello"<<endl;
return 1;
}

But using this too you cannot get it inline in all cases there are few conditions in which it will not be a inline function.
These conditions are mentioned below:-


Courtesy:- MSDN
You cannot force the compiler to inline a function when conditions other than cost/benefit analysis prevent it. You cannot inline a function if:

The function or its caller is compiled with /Ob0 (the default option for debug builds).


The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other).


The function has a variable argument list.


The function uses inline assembly and is not compiled with /Og, /Ox, /O1, or /O2).


Function returns an unwindable object by value and is not compiled with /GX, /EHs, or /EHa).


The function receives a copy-constructed object passed by value, when compiled with /GX, /EHs,, or /EHa.


The function is recursive and is not accompanied by #pragma(inline_recursion, on). With the pragma, recursive functions can be inlined to a default depth of eight calls. To change the inlining depth, use #pragma(inline_depth, n).
May 9 '07 #8
AdrianH
1,251 Expert 1GB
But also you can force ur compiler to make a function inline
using
__forceinline

example
long __forceinline function()
{
cout<<"hello"<<endl;
return 1;
}
This is not portable.
But using this too you cannot get it inline in all cases there are few conditions in which it will not be a inline function.
These conditions are mentioned below:-


Courtesy:- MSDN
You cannot force the compiler to inline a function when conditions other than cost/benefit analysis prevent it. You cannot inline a function if:
...
Looks like it is a MS compiler extension. Please give the reference link.

Thanks,


Adrian
May 9 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
4
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at...
43
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the...
55
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
4
by: firegun9 | last post by:
I am not a newbie C++, but I haven't coded multiple files project under g++. Here is my quesiont: b.h //////////////////// #ifndef b_h #define b_h class b{
3
by: Schizoid Man | last post by:
Hi, I'm a novice whose just about migrating to C++ after cutting my teeth on C for a few years. To start with I'm using Microsoft's out-of-the-box Visual C++ Express Edition compiler and I...
15
by: niklaus | last post by:
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...
25
by: toton | last post by:
Hi, As inline is not mandetory, it depends on compiler to inline certain function (or using switch like fior GCC), my question is there any scope for inlining when it is not declared as inline...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.