c++11 - C++ 库 : . hpp + .inl(单独的定义和声明)vs 仅 .hpp(类内主体代码)

标签 c++11 coding-style libraries

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

目前整个库都是模板化的,只有标题。 这意味着所有代码都在类的主体中。这不是很容易管理,但没关系。

在阅读了几个 C++ 库编码指南后,我很想将其分解为 .hpp + .inl文件。 对一些类进行了实验,它确实提高了可读性,并使其他人更容易处理。我知道在任何给定的时间一切都在哪里。但是其他用户可能想要快速浏览类声明......以及仅在必要时(调试)的定义。

问题 :
将成员定义与类模板的类定义分开有什么优点/缺点?是否有普遍接受的做法。

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

最佳答案

我在另一个问题中找到了答案。
问题: When should I consider making a library header-only? - 和 answer is here^ .
答案是我会把它分解成 .cpp.hpp文件并使其准备好编译为仅 header 和静态库或 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.


^ 这是一个很棒的主意。 这将需要更多的工作,但它是如此多才多艺。
更新
它是 explicitly instantiate^ 很重要 .cpp 中的模板化类文件。

关于c++11 - C++ 库 : . hpp + .inl(单独的定义和声明)vs 仅 .hpp(类内主体代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18064948/

相关文章:

c++ - std::map emplace 不可移动不可复制非默认构造类型

java - 在 Java 中格式化长方法调用的可接受方法是什么?

Python - 在类中声明属性的惯例是什么?

memory - 共享库是否减少了 WebSphere AppServer 7 上的内存占用?

c++ - 新建/删除不匹配

c++ - 如何从头到尾迭代一个unordered_set

c++ - 指向函数成员的指针 : what does `R(*C::*)(Args...)` mean?

c++ - 无锁 fifo 缓冲区中的删除节点检测

库之间的 C 字符串转换

android - 禁用自动滑动。库android-menudrawer