1

CASE 1 (Definition and declaration in same source file)

Suppose both my prototype and definition of global function is in .cpp file. Where should I write inline keyword to make compiler know?

  1. In the prototype
  2. In the definition
  3. Both

CASE 2 (Definition and declaration in same header file)

If I have declared prototypes in .h file I want to make that function inline in more than one .cpp file using it, then should I define the function (to be inlined) in that .h file and include it to individual .cpp files? If yes, then in this case where inline keyword should be used?

CASE 3 (Definition in Header file and declaration in Source file)

If the prototype is in a header file, definition in a source file (in which other functions use the inline function), where must the inline keyword be used?

  1. In the declaration in header file
  2. In the definition in source file
  3. In both
3
  • stackoverflow.com/questions/5057021/… Commented Aug 22, 2013 at 8:03
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Aug 22, 2013 at 8:47
  • @gnat - I don't remember C / C++ syntax well enough to recall if all 3 cases are valid (I don't think so), but I think this is a valid programming design question to be asked here on P.SE. I don't believe that this belongs on SO.
    – user53019
    Commented Aug 22, 2013 at 11:23

2 Answers 2

1

You asked about the syntax of the inline keyword:

Case 1

There's only one definition here, so no possible ODR violation, and no need for inline at all.

Case 2

Just write the prototype and definition in your header file as

inline int foo() { return 42; }

or whatever

Case 3

Like case 1, it isn't clear why you think this function is inline in the first place. Don't use the keyword, there is no risk of ODR violation here.


I now suspect you meant to ask how to force the compiler to generate inlined calls to your global function.

The inline keyword does not force the compiler to do this. It was only ever a hint, and now its only deterministic effect is on the ODR (one definition rule).

The general answer is to turn up the optimization level and, if your compiler doesn't support cross-module inlining, to make the function body visible in-line. The compiler may still decide not to inline the function call even if it's visible, but the optimizer will have the option.

2
  • Thanks. But my question was- how a user make the compiler know that he want to make the function inline in above 3 cases ?
    – sk patra
    Commented Aug 22, 2013 at 16:36
  • That may be what you wanted to know, but it isn't what you asked. Are you just trying to make the compiler inline calls to these functions?
    – Useless
    Commented Aug 22, 2013 at 17:46
0

inline is NOT part of the interface, and client users do not care if a function is inline or not. Basically you can place a inline key word both in the declaration and definition. But it is preferred to place it in the implementation that is in the definition.

So, the rest 3 cases is trivial. You can choose anyone you like. But with the keyword in definition is my prefer.

2
  • thanks.. you mean any one of declaration/definition is sufficient to inform compiler?
    – sk patra
    Commented Aug 22, 2013 at 16:27
  • @skpatra exactly.
    – Joey.Z
    Commented Aug 23, 2013 at 4:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.