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...

2
  • 2
    someone is not making his/her homework Commented Mar 24, 2012 at 16:29
  • I am, but it was on a test, and I still couldn't figure out if this is correct or not. Commented Mar 24, 2012 at 16:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't see the word "before" in your quote, or anything equivalent.
@DavidGrayson: okay I get the point you were trying to make, I edited to reflect it appropriately.
+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.
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 ]

Comments

-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.

1 Comment

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.