When should you use the -inl.h files?

I just noted this element in the Google C ++ Coding Style Guide - and I didn't quite understand.

If I put an inline method or function in a file other than the header included by other files, it will not be a class method; and it will only be used for code bits that include it. So why are there any such -inl.h files?

Also, why do we want to embed long functions at all? (i.e., with the exception of templates, when we need to put code in header files to create an instance)

+8
c ++ header-files header inline
source share
2 answers

This is often done for long function templates. The regular header my_functions.h contains only declarations, and the implementation file my_functions-inl.h contains implementations. The reason is that functional templates cannot be placed in .cpp files . Please note: the Xh file contains the X-inl.h file, and not vice versa.

Other libraries have different naming conventions: for example. Some of the Boost libraries use .hpp for template headers and .ipp for template implementation files.

+7
source share

I just noted this point in the Google C ++ Coding Style Guide, and I didn't quite understand it.

Take this guide with a pinch of salt. Many of the guidelines are designed to help you interact with the legacy Google codebase and are not particularly good tips for general C ++ development.

So why are there any such -inl.h files?

There is no particular good reason; I donโ€™t do it myself. Some people like it because it minimizes the amount of material in the main header file that the header users usually want to read, and separates implementation details that they usually don't care about.

Also, why do we want to embed long functions at all?

Sometimes we have to: template definitions should be available in any translation unit that creates the template instance, so they (usually) should be in the headers.

Sometimes we want: implementing the inline function in the header, we do not need to worry about creating and creating a separate translation unit for it. This may make it more convenient to distribute the library; possibly due to longer assembly time.

+11
source share

All Articles