3

Statement: "Inline-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 it helps anybody...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dudade
  • 71
  • 1
  • 7

3 Answers3

6

Yes it is correct but only partly.It maybe re-framed correctly as follows:

"Inline-functions must be defined in every translation unit(but not necessarily before) in which they are called."

C11++ Standard: §7.1.2.4

An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case.[Note: A call to the inline function may be encountered before its definition appears in the translation unit. —end note]

Why this rationale?

When you declare a function inline basically You are telling the compiler to (if possible)replace the code for calling the function with the contents of the function wherever the function is called. The idea is that the function body is probably small and calling the function is more overhead than the body of the function itself.

To be able to do this the compiler needs to see the definition while compiling the code, where the function is being called. Usually, this is done by adding the definition of the function in an header with the inline specifier and then including the header file in every cpp file where the function is to be called.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 3
    I don't see the word "before" in your quote, or anything equivalent. – David Grayson Mar 24 '12 at 16:28
  • @DavidGrayson: okay I get the point you were trying to make, I edited to reflect it appropriately. – Alok Save Mar 24 '12 at 16:41
  • +1 for the improved answer. I'd like to add another rationale for inline functions: it allows the compiler to optimize the body of the function. Sometimes you can optimize an entire large function down to a couple instructions if you know in advance what the value of the argument will be. – David Grayson Mar 24 '12 at 17:32
0

No. C++11 draft n3242 more clearly than the earlier specifications states in 7.1.2 subsection 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 ]

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
-2

The statement makes no sense: a function that is inlined is no longer called, the code is simply there in the current function (it has been "inlined"). So no, I'd say it's not correct.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • 1
    You can still "call" an inline function by writing "foo()" even if the compiled code doesn't look like a function call. It seems like you're just imposing an artificially strict definition of the word "call". – David Grayson Mar 24 '12 at 16:31