C++ library: .hpp + .inl (separate definitions and declarations) vs .hpp only (in-class body code)

偶尔善良 提交于 2019-12-23 12:19:19

问题


I'm rewriting my Windows C++ Native Library (an ongoing effort since 2002) with public release in mind. For the past 10 years I've been the sole beneficiary of those 150+ KLOC and I feel others might find good uses for it also.

Currently the entire library is quite-templated and header only. That means all code is in the body of the classes. It's not very easy to manage but it's OK.

I'm VERY tempted, after reading several C++ library coding guidelines, to break it into .hpp + .inl files. Experimentally done so for a few classes and it does increase readability and makes it easier for others to deal with. I know where everything is at any given time. But other users might want to a quick overview of a classes declaration... and the definition only if necessary (debugging).

QUESTION:
What are the pros/cons of splitting the member definitions from the class' definition for a class template? Is there a commonly accepted practice.

This is important for me because it's a one way road. I can't refactor it the other way later on so any feedback matters...


回答1:


I've found my answer in another question.

Question: When should I consider making a library header-only? - and answer is here^.

And the answer is I will break it into .cpp and .hpp files and make it ready to compiler both as header only and static library or DLL.

@Steve Jessop:

If you think your non-template library could be header-only, consider dividing it into two files anyway, then providing a third file that includes both the .h and the .cpp (with an include guard).

Then anyone who uses your library in a lot of different TUs, and suspects that this might be costing a lot of compile time, can easily make the change to test it.

^ this is an awesome idea. It will take a bit more work but it's SO versatile.

UPDATE

It's important to explicitly instantiate^ the templated classes in the .cpp files.



来源:https://stackoverflow.com/questions/18064948/c-library-hpp-inl-separate-definitions-and-declarations-vs-hpp-only-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!