inline keyword comparisons for C + + and C languages

Source: Internet
Author: User

In fact, I have always preferred C language, the knowledge of C + + less, but recently because of the need to look at C + + books. In C + + meditation, I saw the phrase: "I also point out that because these member functions are defined within the definition of the trace class itself, C + + expands them inline, so that even without tracking, it does not cost much to keep the trace object in the program." --koenig, "C + + contemplative Chinese version", Page2 ". Mr. K's example code is as follows:

1 class 2 trace{3  Public :4Trace () {noisy =0;}5            voidPrintChar*s) {if(noisy) printf ("%s,"s);} 6            voidOn () {noisy =1;}7            voidOff () {noisy =0;} 8 9 Private:TenNoisy = 1; One}

In fact, this code style is not advocated (the function declaration and definition should be separated), of course, Mr. K here is to give an example to illustrate the problem.

But it did raise my doubts as to why Mr. K said, "Even in the case of non-tracking, there is no cost to keeping the trace object in the program"? Supposedly, this kind of function definition directly in the class inside the way directly led to the function of the inline, and the inline really expansion program, so I produced a doubt, here first planted the foreshadowing , in the knowledge comb after the completion of the attempt to dispel.

The following knowledge is referenced from the CSDN blog, http://blog.csdn.net/lidh04/article/details/3795802 and http://blog.csdn.net/zhangchao3322218/ article/details/8099747.

First, there is inline in the C language, and the instructions here are for the GCC compiler. In GCC, there are three ways of inline, respectively, static Inline,inline,extern inline, which are described separately below:

1.inline. Definition: This is a keyword for the definition of a function ( note is not for the declaration ), for the compiler's recommendation, of course, the compiler has reason to refuse, its role is not specifically for it to generate assembly code at the call, just like a macro to expand.

Note that the following situations are not covered in the above discussion:

First, callback

Second, recursion

The reason for the exception is to ask the reader to think for themselves. Moreover, the function of being such a life can be called outside the file, like this

extern func ();

However, it should be noted that this is not the case:

extern inline function (); // it's not right.

This is the extern inline we are discussing below, the usage is very narrow, please note.

2.static Inline. Can be understood as the superposition of both static and inline properties. As a rule, static limits the function to be used only in the defined file and not extern, as opposed to inline.

3.extern Inline. This cannot be taken for granted as an overlay of extern and inline properties. In fact, the extern inline function of gcc can only be inline, and does not produce a separate assembly code, even if two of the above mentioned accidents occur, and extern inline allows the duplicate name function to appear in the same file.

But we generally have no reason to use such a thing, the meaning of its existence I do not understand now.

Here's a look at the inline in C + +.

In fact, the two languages of the inline is very similar, C + + in the more than the part of C should let us in the beginning of the class of the interior will automatically have already defined functions inline, but this is not worth advocating. At compile time, the inline function eliminates the overhead of function calls (ESP,EBP, register data changes, local changes, context switches, and so on), so smaller inline functions increase performance.

Then we go back to the beginning of the doubts, and try to answer.

In retrospect, my doubts are that Mr. K's "keep the trace object in the program does not have to pay a lot of cost", the reason for confusion is my understanding of the inline: the expansion of functions at the call, causing the program to swell.

But because the function here is very small, the fundamental and expansion of the relationship, we do not recycle the trace object, it will not produce too much waste, that is, Mr. K said "price."

inline keyword comparisons for C + + and C languages

Related Article

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.