Built-in Functions / Methods

Statement: "Internal functions must be defined before they are called."

Is this statement correct?

[EDIT]

The question is originally in German:
Inline-Funktionen müssen vor ihrem Aufruf definiert sein.

Maybe this helps someone ...

+3
source share
3 answers

Yes, that’s right, but only partially. Perhaps it is correctly framed as follows:

"Inline functions must be defined in each block (but not necessarily before) in which they are called. "

C11 ++ Standard: §7.1.2.4

A built-in function must be defined in each translation unit in which it is used, and have exactly the same definition in each case. [Note: a call to a built-in function can be met before its definition appears in the translation block. -end note]

Why is this justification?

When you declare an inline function, you basically tell the compiler (if possible) to replace the code to call the function with the contents of the function wherever the function is called. The idea is that the body of the function is probably small, and the call to the function is more expensive than the body of the function itself.

To do this, the compiler must see the definition when compiling the code where the function is called. This is usually done by adding a function definition to the header using the inline specifier and then including the header file in each cpp file where the function should be called.

+5
source

Not. C ++ 11 draft n3242 more clearly than previous specifications, as stated in clause 7.1.2 of subsection 4;

The built-in function must be defined in each translation unit, in which it is used as odr and must have exactly the same definition in each case (3.2). [Note: a call to a built-in function may be met before its definition appears in the translation block. - end note]

0
source

The statement does not make sense: a function that is built-in is no longer called, the code is simply present in the current function (it was "built-in"). No, I would say that this is not correct.

-1
source

Source: https://habr.com/ru/post/923426/


All Articles