C ++ inline functions inline keyword within

The main function is to inline keyword instead of off macro code fragment.

 Use the keyword inline keyword to declare inline functions in C ++.

inline int Fun ( int A, int B) 
{ 
    return a <b A:? B; // if a <b or a = a established B = A 
}

  

  

#include <stdio.h>

#define FUN(a,b) ((a) < (b) ? (a) : (b))

inline int fun(int a,int b)
{
    return a < b ? a : b; //如果a<b  成立 a=a 否则a=b
}

int main(int argc, char *argv[])
{
    int a = 1,b=3;
    int val = FUN(++a,b);
    printf("Begin...\n");
    printf("val = %d\n",val);
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("End...\n");
    return 0;
}

  If you call a macro FUN    int Val = FUN (++ A, b); expansion is   int val = ((++ a)  <(b) (++ a):? (B)); where Val = 3

  This function is invoked fun  int val = 2

   DESCRIPTION macro block so that there is a drawback that macro block just do substitute content.

 

In C ++ inline inline compiled restrictions:

  1, there is not any kind of loop

  2, excessive conditional statement can not exist

  3, the body of the function can not be too large

  4, the address operation can not be performed taking this function

  5, an inline function declaration must be called before the statement.

 

Inline functions:

C ++ compiler can compile a function inlining

C ++ compiler directly inserted into the body of the function where the function call, so no additional inline function normal function call overhead. (Push, jump, return).

Note: C ++ compiler does not necessarily satisfy the request inline function, that is to say in the use of inline keyword to declare inline functions might be rejected by the compiler . Thereby establishing inline fail

 

Forced inline:

C ++ compiler provides extended syntax, it can be forced to function inlining

  g++ :_attrbute_((always_inline))  属性

  MSVS: _forceinline

 

 

 

Guess you like

Origin www.cnblogs.com/hjxzjp/p/11588122.html