将函数放入头文件的经验法则
最近我开始将越来越多的函数放入头文件中,主要是为了方便。但我担心我可能做得太过分了,我的标题充满了包含内容,我不确定这是否是一个好主意。
将函数移出或移入头文件的经验法则是什么?
如果您想知道,我说的是开发应用程序,而不是库。
编辑:
如果我从我的角度概述内联(自然)标头函数与实现函数的优缺点,我想这会很有帮助:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我最不可违反的规则之一:头文件中只允许内联函数体。其他任何事情都会在链接阶段遇到多个定义的麻烦。
标题应该主要用于声明而不是定义。我对该规则有例外(灵活类型),但它们都不涉及非内联函数体。
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.
我的经验法则是“除非必须,否则不要在标题中。”至于方便性,您觉得增加编译时间方便吗?
My rule of thumb is "Not in the header, unless you have to." And as for convenience, do you find increased compilation times convenient?
有几个明显的技术方面
- 模板和内联函数必须位于标题中
- 多个翻译单元中包含的标题必须警惕“单一定义”规则
- 更坦白地说,你需要一个充分的理由来考虑在标头中放入一个外线函数实现,而且我想不出任何时候我都受到过诱惑。
所以,问题归结为:
头文件中的内联与实现文件中的外联?
因素:
底线:如果您发现自己越来越多地这样做,那么它显然是为了你,没有特别的理由认为你会被烧伤。密切关注潜在的问题,但不要基于一些假设的和不太可能实现的问题而过度设计这些东西。
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:
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.
良好的编码标准将告诉您在源 (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.
既然它已被标记为
C++
,为什么不将它们分成逻辑类
呢?通常,我在头文件中有一个
class
声明,并且它的定义在相应的源文件中。Since this has been tagged as
C++
, why don't you seperate them into logicalclass
es?Normally I have one
class
declaration in a header file and it's definition in the corresponding source file.我使用的两个规则是
1) 如果它是内联函数
2) 如果它是模板函数。
The two rules I use are
1) If it's an inline functions
2) If it's a template function.
首先,模板函数必须放在标题中。
此外,具有空主体的函数,例如默认构造函数或默认但虚拟析构函数,可以放在标头中。
我从不使用内联,因为编译器不能保证这一点。
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.