CASE 1 (Definition and declaration in same source file) Suppose both my prototyp
ID: 654954 • Letter: C
Question
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?
In the prototype
In the definition
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?
In the declaration in header file
In the definition in source file
In both
Explanation / Answer
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.