8

Assuming no requirement to keep implementation private, is there a disadvantage to providing inline functions in header files?

I have read various things around this, but haven't seen a clear answer. What are the advantages and disadvantages of putting inline functions in installed headers for my library? Is there anything I should think about when choosing to do this?

6
  • 1
    Who is doing the inlining? If it's just your library, put them in a private but always-included header. If it's the client, they have to be in a public header, or the client's compiler can't inline them. Commented Jan 20, 2014 at 16:56
  • You may also be interested in cross-posting this question to Programmers StackExchange: programmers.stackexchange.com Commented Jan 20, 2014 at 16:57
  • @peachykeen, well, lets say the inline functions are used throughout the cpp files of the library, but possibly also in other installed headers from the same library. Clients may want to use the inline functions in their own code. Commented Jan 20, 2014 at 16:59
  • @AndyG, is there an automatic way to do that? Commented Jan 20, 2014 at 17:02
  • @crobar: Not that I know of. Unfortunately, I don't think even mods have that kind of power because the different sites in the network aren't integrated that closely. Commented Jan 20, 2014 at 17:04

2 Answers 2

4

It will be a problem if you want to maintain binary compatibility between releases of library. Consider what will happen if you change layout/members of your dynamic library. Your users have some of the methods inlined i their applications (for the old layout). When theirs app will load your library (new version with different layout) those old methods will be called and could cause invalid memory reads/crash.

Btw, in case of libs, it's best to use PIMPL for any userfacing classes.

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

1 Comment

I thought about mentioning this angle myself, but I don't think this problem is exclusive to inlining. This problem is caused by the layout being exposed to the client code, whether the code depending on the layout is in an inline function in the header, or written by the consumer of the library.
3

There is a potential for performance improvements, but you lose many of the benefits of dynamic linking. Normally, existing binaries can be made to benefit from (ABI-compatible) changes, bug fixes and improvements just by updating the shared object file. When some of the library code was inlined into the binary, you're in the same boat as static linking: You need to re-compile all executables to update them.

To be fair, this only affects changes to code in inline functions. But experience suggests that there is some overlap between code that needs to be updated from time to time and code that benefits from being inlined.

5 Comments

Is this a downside of inline functions in general, or just those in installed header files?
@crobar How can the function be inlined if it's not in a header file? Or is there some subtle distinction between "installed header files" and other headers?
Well, I don't need to install every header that is part of my program, only those which are required by users of the library.
@crobar Let me put it like this: Any code that inlines other code is now coupled to said other code. If that code goes into the same shared object file, that's no problem. Otherwise, it's a problem.
Ok, to clarify (I'm a little slow!), If I have a header that is used in my library during compilation which contains inline functions, but the user never sees that header (it's not installed), or those inline functions (they are used only inside the library binary), is that ok? However, in the opposite case (the inline functions are used in some template header file for instance, or part of a header the template file requires for some non-inlined function definition), it's a problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.