将函数放入头文件的经验法则

发布于 2024-11-08 18:32:55 字数 515 浏览 0 评论 0原文

最近我开始将越来越多的函数放入头文件中,主要是为了方便。但我担心我可能做得太过分了,我的标题充满了包含内容,我不确定这是否是一个好主意。

将函数移出或移入头文件的经验法则是什么?

如果您想知道,我说的是开发应用程序,而不是库。

编辑:

如果我从我的角度概述内联(自然)标头函数与实现函数的优缺点,我想这会很有帮助:

Pro inline:

  • 更干净/简洁。
  • 无需复制签名。
  • 无需更改任何 Makefile 即可链接新文件。
  • 即时引入模板参数的能力。

Contra inline:

  • 增加编译时间(我不在乎那么多
  • 许多包含在标头中(如果他们使用防护,就不应该是一个大问题)

据此,这似乎是一个好主意将几乎每个函数都放在标头中,我相信这与 STL 和 Boost 所做的非常接近(尽管它们是库,而不是我的代码)。

Lately I have started to put more and more functions into header files, mostly for convenience. But I fear I might be overdoing it, my headers are full of includes and I'm not sure if that's a good idea.

What are your rules of thumb for moving functions out of or into header files?

In case you're wondering, I'm talking about developing applications, not libraries.

Edit:

I guess it's helpful if I outline the pros/cons of inline (naturally) header functions versus implementation functions from my point of view:

Pro inline:

  • More clean/concise.
  • No need for signature duplication.
  • No need to change any Makefile to link against new files.
  • Instant ability to introduce template parameters.

Contra inline:

  • Increased compile time (I don't care that much)
  • Many includes in headers (Shouldn't be such a big issue if they use guards)

According to that, it seems like a good idea to put pretty much every function in headers, and I believe that's pretty close to what the STL and Boost are doing (although those are libraries, as opposed to my code).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

少年亿悲伤 2024-11-15 18:32:55

我最不可违反的规则之一:头文件中只允许内联函数体。其他任何事情都会在链接阶段遇到多个定义的麻烦。

标题应该主要用于声明而不是定义。我对该规则有例外(灵活类型),但它们都不涉及非内联函数体。

One of my most inviolable rules: only function bodies which are inline are allowed in header files. Anything else is asking for trouble with multiple definitions in the link phase.

Headers should be left predominantly for declarations rather than definitions. I have exceptions to that rule (being the flexible type) but none of them involve non-inlined function bodies.

心病无药医 2024-11-15 18:32:55

我的经验法则是“除非必须,否则不要在标题中。”至于方便性,您觉得增加编译时间方便吗?

My rule of thumb is "Not in the header, unless you have to." And as for convenience, do you find increased compilation times convenient?

朱染 2024-11-15 18:32:55

有几个明显的技术方面
- 模板和内联函数必须位于标题中
- 多个翻译单元中包含的标题必须警惕“单一定义”规则
- 更坦白地说,你需要一个充分的理由来考虑在标头中放入一个外线函数实现,而且我想不出任何时候我都受到过诱惑。

所以,问题归结为:

      头文件中的内联与实现文件中的外联?

因素:

  • 您说您正在设计应用程序级代码而不是库,因此,您(当前)不必担心其他团队会依赖您的代码,也不必通过保持实现脱节来最大程度地减少重新编译(而不是重新链接)的需要
    • 但是,如果您正在编写有可能对其他团队有用的优秀代码,那么您可能会发现自己希望将实现保持私有
  • 内联与外联通常代表大约一个顺序 -琐碎数据获取/设置函数的巨大开销...如果您有从性能关键代码中重复调用的函数,那么您有理由更喜欢内联
  • 标头内实现(特别是与声明混合在一起时)通常会混淆 API ,但有时实际上使代码更加自我记录
  • 本地化并删除冗余(组合声明/定义)肯定会消除打字错误/错误的可能性,并且通常可以提高生产力

底线:如果您发现自己越来越多地这样做,那么它显然是为了你,没有特别的理由认为你会被烧伤。密切关注潜在的问题,但不要基于一些假设的和不太可能实现的问题而过度设计这些东西。

There are a few obvious technical aspects
- templates and inline functions must be in headers
- headers included from multiple translation units must be wary of the One Definition Rule
- more bluntly, you'd want a bloody good reason to even consider putting an out-of-line function implementation in a header, and I can't think of any times I've even been tempted.

So, the question boils down to:

    inline in header versus out-of-line in implementation file?

Factors:

  • you say you're designing application level code not libraries, so you don't (currently) have to worry about other teams getting dependent on your code, nor minimise their need to recompile (versus just relink) by keeping implementation out of line
    • BUT if you're writing good code that has any potential to become useful to other teams, then you might find yourself wishing you'd kept implementation private
  • inline versus out-of-line typically represents about an order-of-magnitude overhead for trivial data get/set functions... if you have functions that are called repeatedly from performance critical code, then you've reason to prefer inlining
  • in-header implementation (especially if intermingled with the declarations) can often obfuscate the API, but sometimes actually makes the code more self-documenting
  • localisation and removed redundancy (of combining declaration/definitions) definitely removes potential for typos/errors and can often improve productivity

Bottom line: if you're finding yourself doing it more and more, then it's obviously working for you and there's no particular reason to think you're about to get burnt. Keep an eye out for the potential issues but don't over-engineer the heck out of stuff based on some hypothetical and unlikely-to-materialise concern.

笨死的猪 2024-11-15 18:32:55

良好的编码标准将告诉您在源 (cpp) 文件中实现方法和函数。

如果您愿意,可以在标头中实现模板和内联函数。

A good coding standard will tell you to implement methods and functions in the source (cpp) file.

If you prefer it, you can implement templates and inline functions in the header.

雅心素梦 2024-11-15 18:32:55

既然它已被标记为C++,为什么不将它们分成逻辑呢?

通常,我在头文件中有一个 class 声明,并且它的定义在相应的源文件中。

Since this has been tagged as C++, why don't you seperate them into logical classes?

Normally I have one class declaration in a header file and it's definition in the corresponding source file.

彻夜缠绵 2024-11-15 18:32:55

我使用的两个规则是

1) 如果它是内联函数

2) 如果它是模板函数。

The two rules I use are

1) If it's an inline functions

2) If it's a template function.

够钟 2024-11-15 18:32:55

首先,模板函数必须放在标题中。

此外,具有空主体的函数,例如默认构造函数或默认但虚拟析构函数,可以放在标头中。

我从不使用内联,因为编译器不能保证这一点。

First, template function must be put in headers.

Besides, functions with an empty body, such as a default constructor or default but virtual destructor may be put in headers.

I never use inline because compiler don't guarantee that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文