Question about Inline functions.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abhinay
    New Member
    • Jul 2006
    • 44

    Question about Inline functions.

    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:
    Code:
    class Test
    {
    	
    public:
    	Test(){}
    	inline long print();
    	inline void afterPrint();
    };
    
    long inline Test::print()
    {
    	cout<<"In side Print "<<endl;
    	return lNo;
    }
    
    void inline Test::afterPrint()
    {
    	cout<<"In side afterPrint "<<endl;
    	
    }
    
    void main()
    {
    	Test test;
    	test.print();
    	test.afterPrint();
    }
    Output:

    In side Print
    In side afterPrint

    Regards
    Abhinay
    Last edited by AdrianH; May 8 '07, 02:02 PM. Reason: Please use [code][/code] tags for easier reading!!
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    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

    Comment

    • ajayraj
      New Member
      • May 2007
      • 21

      #3
      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.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by Abhinay
        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

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by Abhinay
          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:
          Code:
          int inline funct()
          {
              return 5;
          }
          
          int main()
          {
              int x = funct();
              
          }
          will be compiled as:
          Code:
          int main()
          {
              int x = 5;
              
          }
          so after the return of funct() you are still in main(). That is why your prgram is not terminated.

          Comment

          • JMathews
            New Member
            • Apr 2007
            • 10

            #6
            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.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by JMathews
              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

              Comment

              • ajayraj
                New Member
                • May 2007
                • 21

                #8
                Originally posted by AdrianH
                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).
                Last edited by ajayraj; May 9 '07, 05:25 AM. Reason: spelling mistake

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by ajayraj
                  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.
                  Originally posted by ajayraj
                  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

                  Comment

                  Working...