Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an 'extern inline' function and when to use?

Tags:

c++

c++14

There is such thing and it's in the latest C++ draft:

At § 7.1.2 .4:

An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [ Note: A call to the inline function may be encountered before its definition appears in the translation unit. — end note ] If the definition of a function appears in a translation unit before its first declaration as inline, the program is ill-formed. If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. An inline function with external linkage shall have the same address in all translation units. A static local variable in an extern inline function always refers to the same object. A type defined within the body of an extern inline function is the same type in every translation unit.

Some insights on what is this and when to use it?

like image 702
AnArrayOfFunctions Avatar asked Dec 24 '14 22:12

AnArrayOfFunctions


People also ask

What is extern inline?

The extern-inline module supports the use of C99-style extern inline functions so that the code still runs on compilers that do not support this feature correctly. C code ordinarily should not use inline .

What is inline function when and why it is used?

Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.

Can inline functions extern?

Similarly, if you define a function as extern inline , or redeclare an inline function as extern , the function simply becomes a regular, external function and is not inlined. End of C only. Beginning of C++ only.

What is the advantage of inline?

Advantages :- 1) It does not require function calling overhead. 2) It also save overhead of variables push/pop on the stack, while function calling. 3) It also save overhead of return call from a function. 4) It increases locality of reference by utilizing instruction cache.


1 Answers

extern is redundant for functions, so it is pointless to declare a function extern inline. If, for example you declared a function inline at global scope, the rules of this section would apply. Likewise if you declared a class at global scope and defined a member function within the class definition, as such a function would be implicitly inline.

The question of when you should declare a function inline is a can of worms I'm not inclined to open here. See: When should I write the keyword 'inline' for a function/method?

like image 172
Brian Bi Avatar answered Oct 02 '22 20:10

Brian Bi