C ++库:.hpp + .inl(单独的定义和声明)vs .hpp(类内部代码) [英] C++ library: .hpp + .inl (separate definitions and declarations) vs .hpp only (in-class body code)

查看:1821
本文介绍了C ++库:.hpp + .inl(单独的定义和声明)vs .hpp(类内部代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是重写我的Windows C ++本机库 (自2002年以来一直在努力)公开发布。在过去的10年里,我一直是这150多个KLOC的唯一受益者,我觉得其他人也可能找到很好的用途。



库只是相当模板和头文件。这意味着所有代码都在类的正文中。



我很感冒,在阅读了几个C ++库编码指南之后,将它分成 .hpp + .inl 文件。对几个类进行实验性测试,其他人处理。我知道在任何给定的时间一切都在哪里。但其他用户可能希望快速了解类声明...以及仅在必要时进行定义(调试)



QUESTION

将类模板的类定义中的成员定义拆分的优点/缺点是什么?是否有普遍接受的做法。


这对我来说很重要,因为它是一条单向的道路。 任何反馈事项 ...



解决

http://stackoverflow.com/questions/6164817/when-should-i-consider-making-a-library-header-only\">我应该考虑制作图书馆标题吗? - 并 回答在此处^



然后答案是我将它分成 .cpp code> .hpp 文件,并准备好作为头文件和静态库或DLL的编译器。




@ Steve Jessop



如果您认为您的非模板库只能是标题,请考虑将其分为两个文件,然后提供第三个文件,其中包含 .h .cpp (包含附加警告)。



TU,并怀疑这可能花费大量的编译时间,可以轻松地进行更改测试。


^



更新 / strong>



显式实例化^ .cpp 文件中的模板化类。


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...

解决方案

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.

这篇关于C ++库:.hpp + .inl(单独的定义和声明)vs .hpp(类内部代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆