I already know that, when I put the definition of a member function into a header and mark the function as inline
, the code in the function gets inlined into any place where the function is called out of a .cpp file, so when it comes to a compiled binary, I know where the function's code is located -- within the compiled code of any .cpp file that depends on it. But what happens if I don't mark a function in a header with inline
and the function's body is large enough to make the compiler choose not to inline it? In the context of a static/dynamic library the function's class belongs to, where does the function's code is compiled to? Or is it not compiled at all and the final destination for the function's code is a compiled .cpp of a client of the library? If it's the latter case, does the function's code still gets inlined even if I didn't mark it with inline
(because its code was too "heavy")? And finally, is MSVC compiler's behavior in this case differs from the GCC's one?
And sure, I realize that putting member functions I want to be inlined into .h file (or .inl file) and "heavy" function into .cpp file would make things crystal clear, but I would really like to avoid breaking a class' implementation across files, hence is the interest.
When you mark a function inline
you're not forcing the compiler to inline it at every place it's called, you're just telling it that that the definition is inline and it should expect duplicate copies in different compilation units. The actual code will be compiled at least once for every compilation unit where you include the header and call the function.
If you don't declare it inline, the linker should complain about multiple definitions of the function, even if those definitions are identical.