The difference and use __c++ of inline/static Inline/extern Inline in C + +

Source: Internet
Author: User
Tags class definition diff function definition

The purpose of introducing the inline function is to solve the efficiency problem of function call in the program, and to replace the parameter macro definition with the inline function (function parameters are more convenient to use than macros).

The inline keyword is used to define an inline function for a class.

There is a difference between defining a member function in the class body and the class body: the member function defined in the class body is an inline (inline) function that is not defined outside the class body. If you want to define a function outside of the class body, if you want it to be an inline function, then you can add the inline keyword when declaring the function. functions defined within the class body can also be added to the inline keyword, but this is superfluous, because the function defined within the class body defaults to the inline function. If you define in the class body inline function, you must place the definition of the class definition and member function in the same head file (or in the same source file), otherwise it cannot be embedded at compile time.

Inline the inline function of the class defined, the code of the function is put into the symbol table, replaced directly in use (like a macro), without the overhead of the call, and the efficiency is high. The inline function of a class is also a true function, and when the compiler calls an inline function, it first checks the type of its arguments to ensure that the call is correct. Then do a series of related checks, just like any real function. This eliminates its hidden dangers and limitations. Inline can be a member function of a class, and of course you can use the protection and private members of the class in it.

Inline usage Scenarios: (1), you can use the inline function to completely replace the expression form of the macro definition, (2), inline functions are generally only used in the content of the function is very simple, this is because the code of the inline function in any call it to expand, if the function is too complex, The consequences of code bloat are likely to outweigh the benefits of increased efficiency.

At compile time, the compiler replaces the invocation expression of the inline function that appears in the program with the function body of the inline function. Obviously, this practice does not produce the problem of turning back, however, because the code in the function body is substituted to the program at compile time, it increases the amount of the target program code, which increases the space overhead, and it is not as big as the function call in the time cost, it is obvious that it is the cost of increasing the target code in exchange for time saving. Inline functions reduce CPU overhead and the overall speed of the program accelerates, but when the inline function is large, it has the opposite effect, so generally smaller functions use inline functions.

There are two kinds of declarative methods of inline functions, one is to use the inline keyword before the function, the other is to define the code of the function inside the class, such functions will automatically convert to inline functions, and there is no need to place inline in front of the function.

Inline is a request to the compiler, which prevents the compiler from obeying the request: If the function contains loops, switch or goto statements, recursive functions, which contain static functions.

It can be seen that the inline function and the member function is no different, the difference is how to speed up the execution of the function. Inline functions are a waste of space to save time settings, because the function of the call is a waste of time, written inline function can be used in each call with function body content instead of function calls, a bit like a macro definition. The inline function can be used when the function body statement is less and there is no complex circular statement and the number of calls is more.

The inline function looks at the source layer, has the function structure, but does not have the function nature after compiling. At compile time, similar to macro substitution, use function body to replace function name at call. Generally in code with inline modification, but whether the inline function can be formed, you need to see the compiler's definition of the function of the specific processing.

"Static inline" means "we have to have this function, if you use it, but don ' t inline it, then make a static version of it In the compilation unit ". "extern inline" means "I actually _have_ an extern for this function, but if your want to inline it, here ' s the Inline-vers Ion ".

Static is the use of the previous C. The purpose is to make the function identified by this keyword visible only in local files, and other files of the same program are invisible to that function. In other words, even if your other files contain a function definition with the same name as the parameter table, is also an error that does not cause duplicate definitions of functions. Because static is visible only in the current file.

Static inline, an inline function that does not use a function call, inserts the assembly code directly into the call to the function.

Static inline, you can think of it as a static function, plus the inline properties. The static inline function, like the static function, has a defined scope that is local, that is, you can have several different definitions within the program (as long as it is not within the same file).

The inline function of the static inline, usually does not produce the code of the function itself, but is all embedded in the called Place. If you do not add static, the function may be invoked by other compilation units, so the code for the function itself must be generated. So the static is added, which generally makes the executable file smaller. Most of the inline functions used by the Linux kernel are defined as static types. A "static inline" function prompts the compiler to attempt to insert its code into all programs that invoke it.

extern inline indicates that the function has been declared. Because the function itself can be declared multiple times, the effect of extern on a function is only explicit to the hidden attribute of the function. extern is useful for objects that are not functions, because an object declaration brings memory allocations, and extern means that the object has been declared and no more memory is allocated.

The application scope of the extern inline function is narrower and generally not recommended.

The above content is mainly organized from:

1. http://www.cnblogs.com/pengyingh/articles/2405718.html

2. Http://stackoverflow.com/questions/216510/extern-inline

3. Http://wenku.baidu.com/link?url=QVc0SQBmFKDjcz65nQRe5wqsaY-rBp89VAu9iX6aCm2avMORW-IeGf_ Zyxfyrf4vni7p-g3efc7sx_fzzuh2r2eoxegajf9mwxvm1_sjsvo

The following is the test code:

Static_inline.h:

#ifndef fbc_messy_test_static_inline_hpp_
#define FBC_MESSY_TEST_STATIC_INLINE_HPP_

class Fastmath {
Public:
	the function defined internally by the int round (float value) {//class body is either an inline function or a inline keyword, but this is an extra return
		(int) (value + >= 0?). 0.5f: -0.5f));
	}

	inline int floor (float value); Declared as inline function
};

int Fastmath::floor (float value)
{
	int i = round (value);
	float diff = (float) (value-i);
	return i-(diff < 0);
}

static inline int ceil (float value)
{
	int i = (int) (value + (value >= 0 0.5f: -0.5f));
	float diff = (float) (i-value);
	return i + (diff < 0);
}

void Test_static_inline1 ();
void Test_static_inline2 ();

#endif//Fbc_messy_test_static_inline_hpp_
Static_inline.cpp:

#include "static_inline.hpp"
#include <iostream>

void test_static_inline1 ()
{
	float a = 4.1, B = 4.9;

	Fastmath Math;
	int ret1 = Math.Round (a);
	int Ret2 = Math.floor (a);
	int ret3 = Math.Round (b);
	int ret4 = Math.floor (b);

	Std::cout << ret1 << "   << ret2 <<"    "<< ret3 <<"    "<< ret4 &L t;< Std::endl;
}

void Test_static_inline2 ()
{
	float a = 4.1, B = 4.9;

	int ret1 = Ceil (a);
	int Ret2 = ceil (b);

	Std::cout << ret1 << "    << ret2 <<"    "<< Std::endl;
}

GitHub: Https://github.com/fengbingchun/Messy_Test

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.