When should I use -inl.h files?

后端 未结 3 790
有刺的猬
有刺的猬 2021-02-14 18:19

I just noted this item in the Google C++ Coding Style Guide - and I don\'t quite get it.

If I put an inline method or function in a file other than the header included b

3条回答
  •  伪装坚强ぢ
    2021-02-14 18:41

    I just noted this item in the Google C++ Coding Style Guide - and I don't quite get it.

    Do take that guide with a pinch of salt. Many of the guidelines are intended to help interact with Google's legacy codebase, and aren't particularly good advice for general C++ development.

    So why even have such -inl.h files at all?

    There's no particularly good reason; I don't do that myself. Some people like them because it minimises the amount of stuff in the main header file, which users of the header generally want to read, and separates out the implementation details, which they generally don't care about.

    Also, why do we even want to inline long functions anyways?

    Sometimes, we must: template definitions must be available in any translation unit that instantiates the template, so they (usually) need to be in headers.

    Sometimes, we want to: by implementing a function inline in a header, we don't have to worry about building and linking a separate translation unit for it. This can make it more convenient to distribute a library; possibly at the cost of longer build times.

提交回复
热议问题