inline functions, default parameters, and function placeholder parameters

Source: Internet
Author: User

inline functions

Definition: Inline functions from the source code layer, there is the structure of the function, but after compiling, but does not have the nature of the function. The inline function does not take control over the call, but instead embeds the body of the function at compile time at each call. At compile time, similar to macro substitution, use the function body to replace the function name at the call. It is generally decorated with inline in the code, but the ability to form inline functions depends on the compiler's specific handling of the function definition.

A const constant in C + + can override the definition of a macro constant, such as:

const int A = 3;? #define A 3

Is there a solution in C + + that replaces the macro code snippet? (instead of macro code snippets you can avoid the side effects of macros!) )

Inline functions are recommended for use in C + + to replace macro code snippets.

The inline keyword is used in C + + to declare an inline function.

The inline keyword must be combined with the function definition when the inline function is declared, or the compiler ignores the inline request directly.

#include "iostream" using namespace std; #define MYFUNC (A, B) ((a) < (b)? (a): (b) inline int myfunc (int a, int b) {return a < b a:b;} int main () {    int a = 1;    int b = 3;    int c = MyFunc (++a, b);    int c = MYFUNC (++a, b);    printf ("A =%d\n", a);     printf ("B =%d\n", b);    printf ("c =%d\n", c);    printf ("Press ENTER to continue ...");    System ("pause");    return 0;}

Illustration 1:

must be inline int myfunc (INTA, int b) and implement the place, written in a piece.

Illustration 2:

The C + + compiler can compile a function inline, and a function compiled inline by the C + + compiler is called an inline function;

Inline functions are not defined in the resulting code;

C + + the compiler directly inserts the function body into the function call place;

The inline function does not have the extra overhead of a normal function call (stack, jump, return).

Illustration 3:

The C + + compiler does not necessarily allow inline requests for functions!

Illustration 4:

An inline function is a special function that has the characteristics of a common function (parameter checking, return type, etc.).

An inline function is a request to the compiler, so the compiler might reject the request.

The inline function is handled by the compiler, inserting the compiled function body directly into the calling place.

Macro Code Snippetprocessed by the preprocessorFor simple text substitution without any compilation process.

Illustration 5:

The modern C + + compiler is capable of compiling optimizations, so some functions may be compiled inline, even without inline declarations.

In addition, some modern C + + compilers provide extended syntax to enforce inline functions such as the __attribute__ ((always_inline)) attribute in g++.

Illustration 6:

Limitations of inline compilation in C + +:

There cannot be any form of circular statement;

There cannot be too many conditional judgment statements;

function body can not be too large;

The function can not be taken to the site operation;

The function inline declaration must precede the calling statement;

The compiler's limitations on inline functions are not absolute, and the benefits of inline functions relative to normal functions simply omit the overhead of stack, jump, and return on function calls. Therefore, inline is meaningless when the execution overhead of the function body is much larger than the stack, jump, and return overhead.

Conclusion:
1) The inline function inserts the function body directly into the function call place at compile time.

2) Inline is just a request, and the compiler does not necessarily allow this request.

3) The inline function eliminates the overhead of pressing stacks, jumps, and returns during normal function calls.
Default parameters

In C + +, you can provide a default value for a parameter when the function is declared, and when the function call does not specify a value for the parameter, the compiler automatically replaces it with the default value.

void Myprint (int x = 3) {printf ("x:%d", x);}

Rules for function default parameters:

Only parameters that are later in the parameter list can provide default parameter values.

Once you start using the default parameter values in a function call, all parameters after this parameter must use the default parameter values.

Function-Placeholder Parameters

The placeholder parameter is only declared with the parameter type, not the parameter name, and in general, the placeholder parameter cannot be used inside the body of the function.

int func (int a, int b, int) {return a + B;} int main (int argc, char *argv[]) {printf ("func (1, 2, 3) =%d\n", func (1, 2, 3));p rintf ("press ENTER to continue ..."); getch AR (); return 0;}
Reprint Please specify: http://blog.csdn.net/lsh_2013/article/details/45421293




inline functions, default parameters, and function placeholder parameters

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.