C + + inline functions, function templates to header files

Source: Internet
Author: User

I. BASIC INSTRUCTIONS

As mentioned in the C + + standard, a compilation unit refers to a. cpp file and all the. h files it contains, and the code in the. h file is extended to the. cpp file containing it, and the compiler compiles the. cpp file as an. obj file, which has a PE[ Portableexecutable, which is the Windows executable file format, and itself contains the binary code, but it is not necessarily able to execute, because there is no guarantee that there must be a main function. When the compiler compiles all the. cpp files in a project in a separate way , the connector (linker) connects to an. exe file.

Template functions

Template <typename t>const t& comparemax (const t &a, const T &b) {return a > B? a:b;}

The template function is an extract of the generic operation of the approximate type, which is then instantiated (special) according to the type during compilation, because the function type can be checked during compilation.

inline functions

inline int Comparemax (int a, int b) {return a > b a:b;}

The inline function must be visible to the compiler so that it can expand the function within the point of the call. Unlike non-inline functions, the inline function is not a jump for a function, but rather an expansion of the instruction (thus improving execution efficiency). If the inline function is too large, it will cause the target code to be too large, increase the additional page break behavior, and reduce the hit rate of the instruction cache device.

Second, the head file

1. Difference & Contact

Template functions are generally declared as inline functions, but are not required. One is the function expansion of the calling point, and one is an overload of the function based on the type. two features do not necessarily stick to the reason . That is, the function template executes first, through the function template to produce a series of functions, specifically to take these functions as function calls or as inline expansion, it depends on whether the application is accepted by the compiler.

Template functions need to be instantiated, and inline functions do not necessarily need to be instantiated (they need to be instantiated when using an inline function pointer).

2. header file

The template function is placed in the header file because the compiler checks the type, and the compiler sees the function implementation in order to instantiate the template. If the Declaration and implementation of the template placed in the function are separated, then the template implementation will not be found and then a link error is raised (this is the current compiler)

The inline function is placed in the header file because it is convenient to unify, if the definition and declaration of the inline function are separate, and when the inline functions need to be called in another file, the inline cannot be expanded within these calling functions (the second example above) can only be called. In this way, the inline function loses its role in the global scope.

Third, code example

Two files:
The MAIN.C code is as follows

#include <stdio.h> #include "print_inline.h" int main (int argc, char *argv[]) {   print_inline (); System ("PAUSE" );   return 0;}
Code in the Print_inline.h file:
#include <stdio.h>inline void Print_inline () {    printf ("This is a inlinefunction\n");      
At the time of preprocessing, the Main.c file will be Print_ Inline.h header file expansion, under DEVC, the preprocessed file is main.i, (if you want to generate pre-processed files, need to be in the project properties, the compiler parameters-c-save-temps) get the following preprocessing results (the file is longer, only the last part):
# 3 "main.c" 1 "print_inline.h" 1inline void Print_inline () {    printf ("This is a inline function\n");} # 4 "Main.c" 2int main (int argc, char *argv[]) {print_inline (); System ("PAUSE"); return 0;}

  

Clearly, the function print_inline () defined in the Print_inline.h header file is directly expanded in the main function, quite as we put the Print_inline () The definition of the function is placed in the main.c so that, at compile time, the compiler can inline the Print_line () function directly into the main function.

But if we separate the declaration and definition of the print_inline () function, that is, the definition of the Print_inline () function is placed in another file print_inline.c, the result is different, and the content in the Main.i file becomes
# 3 "main.c" 1 "print_inline.h" 1inline void Print_inline (); # 4 "Main.c" 2int main (int argc, char *argv[]) {Print_inline (); System ("PAUSE"); return 0;}

  

At this point, the Print_inline () function will not be inline in the main function, and we can see that the generated MAIN.S assembly code contains the following
Code:
LM3:   Call   _print_inline

In fact, the principle is very simple, it is equivalent to include a file with # # time, the preprocessing time will directly expand the file, if the file has a definition of a function, in fact, it is equivalent to the definition of the function in the inclusion of this file (the above example of the Print_ inline.h) file (MAIN.C), so you can expand the Print_inline function inline in main.

In many cases, because some functions need to be called frequently, in order to speed up the execution of the program, it is often necessary to use inline, but if the definition and declaration of the inline function are separate, and when the inline functions need to be called in another file, Inline is not expandable within these calling functions (the second example above), can only be called. In this way, the inline function loses its role in the global scope. The solution is to put the inline function definition in the header file, when the other files to invoke these inline functions, as long as the inclusion of the header file is possible.

C + + inline functions, function templates to header files

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.