C + + Inline

Source: Internet
Author: User

C + + inline functions

1 int big (intint  b) {2     return a > B a:b; 3 }


The above is the function for the larger of the two numbers, and of course we can use the position of the big (A, B) as follows: (a > B? a:b).

The advantages and disadvantages of defining this small operation as a function are as follows:
Advantages:
1. It is easier to read and understand a function's invocation than to read an equivalent expression.
2. The use of functions ensures that the behavior is uniform, and each related operation is guaranteed to proceed in the same manner.
3. Modifying a function is easier than modifying an equivalent expression for all locations.
4. Functions can be reused by other applications, eliminating the cost of coding.
Disadvantages:
1. Calling a function is slower than finding an equivalent expression.
A function call consists of a series of functions: Save the Register before the call and restore it on return; you may need to copy the arguments; The program moves to a new location to continue execution.

Inline functions to avoid the overhead of function calls
Specifies that a function is inline, usually by expanding it "inline" on each invocation. Let's say the big function is set
If the literal is inline, the following call
cout << Big (A, b) << Endl;
Expands into a form similar to the following during compilation
cout << (a > B a:b) << Endl;

1. Inline functions are only a suggestion for the compiler, and the compiler can choose to ignore this recommendation.
2. The inline function should be defined in the header file, which is different from the other functions.
3. When adding or modifying an inline function in a header file, all source files that use the header file must be recompiled.
4. The definition of an inline function must be visible to the compiler so that the compiler can expand the code of the function within the calling point.
5, in order to make inline declaration inline function is valid, we must put the inline keyword together with the function body, otherwise the inline keyword is not able to successfully declare a function inline function.

MORE:https://zh.wikipedia.org/wiki/%E5%86%85%E8%81%94%E5%87%BD%E6%95%B0

C + + Inline

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.