[Programming Pearl] inline functions and macros

Source: Internet
Author: User

I. Usage of inline functions


From the source code layer, inline functions have a function structure, but are not functional after compilation. During compilation, a macro-like replacement is used to replace the name of the called function with the function body. Inline is usually used in code, but whether an inline function can be formed depends on the specific processing of the compiler's definition of the function.


Inline tablefunction (int I) // it is ineffective. The Compiler just uses the function as a general function declaration and we must define the function body.

Inline tablefunction (int I) {return I * I}; // This defines an inline function. We can call it as a common function. However, the execution speed is indeed faster than that of common functions.



Inline functions are valid only when they are declared together with the function body.


[Note] We can also define functions defined outside the class as inline functions, such:

Class TableClass{private:  int i,j;public:  int add() { return i+j;};  inline int dec() { return i-j;}  int GetNum();}inline int tableclass::GetNum(){  return i;}

  

All the three functions stated above are inline functions. In C ++, the function of the function body is defined inside the class and is considered as an inline function by default. Whether or not you have the inline keyword.


2. inline function applications (Private Members of objects can be accessed)

Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. To read and write private or protected members, you must use the member interface function. If we define these read/write member functions as inline functions, the efficiency will be better.

Class sample{private:  int nTest;public:  int readtest(){ return nTest;}  void settest(int i) {nTest=i;}}

  

Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions.


3. Considerations for inline functions

1. Loop statements and switch statements are not allowed in inline functions. Otherwise, compile the function as a normal function.

2. recursive functions (self-called functions) cannot be used for inline functions.

3. the inline function is only applicable to 1 ~ Five rows of small functions. For a large function that contains many statements, the overhead of function calling and return is relatively insignificant, so it is not necessary to use inline functions.

4. the inline function must be defined before the first call of the inline function.

5. In the class structure, all functions internally defined in the class description are inline functions.


Iv. macro definition

Macro definitions with or without parameters. Specifically, a designated identifier is used to replace a string or describe the replacement.

Macro expansion: the process of replacing the macro name with a string during C program Compilation

# Define max (A, B) (a)> (B ))? (A) :( B)


5. Macro limitations


1. Macros cannot access private members of objects.

2. The definition of macros is prone to ambiguity.


Example of ambiguity:

# Define table_multi (x) (x * X)

Table_multi (10) // 100 is returned, which is correct.

Table_multi (10 + 10) // The expected result is 400, while the macro call result is (10 + 10*10 + 10) and the result is 120.


This is obviously not the expected result. To avoid these errors, add brackets to macro parameters.


# Define table_multi (x) * (x ))

This ensures that no error is made. However, even if this definition is used, this macro may still fail.

Table_multi (A ++) // we expected to get the result (a + 1) * (a + 1), and the expanded result of the macro: (A ++) * (a ++)

If the value of A is 4, the result is 4*4 = 16, A = 6. What we expect is 5*5 = 25


Vi. Differences between inline functions and macros

Macros are replaced by the pre-processor, and inline functions are implemented by the compiler.

Inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead.

You can call inline functions like calling functions without worrying about macro processing issues.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.