将C ++定义放在头文件中是一种好习惯吗? [英] Is it a good practice to place C++ definitions in header files?

查看:131
本文介绍了将C ++定义放在头文件中是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我个人的C ++风格总是将类声明放在一个include文件中,并将定义放在一个.cpp文件中,非常类似于中的规定. Loki对 C ++头文件,代码分隔 的回答.诚然,我喜欢这种样式的部分原因可能与我过去对Modula-2和Ada进行编码所花费的时间有关,这两者在规范文件和主体文件方面都有相似的方案.

我有一个同事,比我更懂C ++,他坚持认为所有C ++声明应尽可能在头文件中包括定义.他并不是说这是一种有效的替代样式,甚至不是一种更好的样式,而是这是每个人现在都在使用C ++的新的普遍接受的样式.

我不像以前那样弯腰,所以我真的不急于爬上他的这个潮流,直到我看到有更多的人和他在一起.那么这个成语到底有多普遍?

只是给出一些答案的结构:现在是方法了吗,很常见,有些常见,不常见或错误提示?

解决方案

您的同事是错误的,通常的方法是并且一直以来都是将代码放入.cpp文件(或您喜欢的任何扩展名)中,并将声明放在标头中./p>

有时将代码放入标头中有一些好处,这可以使编译器进行更巧妙的内联.但是同时,由于每次编译器包含所有代码时,都必须处理所有代码,因此这可能会破坏您的编译时间.

最后,当所有代码都是标头时,具有循环对象关系(有时是理想的)通常很烦人.

最重要的是,你是对的,他是错的.

编辑:我一直在考虑您的问题.在一种情况下,他的说法是正确的.模板.许多新的现代"库(例如boost)都大量使用模板,并且常常是仅标头".但是,只有在处理模板时才应这样做,因为这是处理模板时的唯一方法.

有些人想要澄清一下,这是编写仅标头"代码的缺点:

如果您四处搜索,您会发现很多人试图找到一种方法来减少处理boost时的编译时间.例如:如何使用Boost Asio减少编译时间,看到包含增强功能的单个1K文件的14s编译. 14s似乎并没有爆炸",但肯定比典型的要长得多,并且累积起来很快.在处理大型项目时.仅标头的库确实以相当可衡量的方式影响编译时间.我们只是容忍它,因为升压非常有用.

此外,还有很多事情不能仅在标头中完成(即使boost具有某些线程,文件系统等某些部分都需要链接到的库).一个主要的例子是,仅标头库中不能有简单的全局对象(除非诉诸于单例的可憎),否则会遇到多个定义错误. 注意: C ++ 17的内联变量将使该示例在将来可行.

最后一点,当使用boost作为仅标头代码的示例时,常常会遗漏大量细节.

Boost是库,而不是用户级代码.因此它不会经常更改.在用户代码中,如果将所有内容放在标头中,则每个小的更改都将导致您不得不重新编译整个项目.这是巨大的时间浪费(对于不从编译到编译不变的库而言,情况并非如此).当您在标头/源和更好之间进行拆分时,使用前向声明减少包含,可以在一整天的总和中节省重新编译的时间.

My personal style with C++ has always to put class declarations in an include file, and definitions in a .cpp file, very much like stipulated in Loki's answer to C++ Header Files, Code Separation. Admittedly, part of the reason I like this style probably has to do with all the years I spent coding Modula-2 and Ada, both of which have a similar scheme with specification files and body files.

I have a coworker, much more knowledgeable in C++ than I, who is insisting that all C++ declarations should, where possible, include the definitions right there in the header file. He's not saying this is a valid alternate style, or even a slightly better style, but rather this is the new universally-accepted style that everyone is now using for C++.

I'm not as limber as I used to be, so I'm not really anxious to scrabble up onto this bandwagon of his until I see a few more people up there with him. So how common is this idiom really?

Just to give some structure to the answers: Is it now The Way, very common, somewhat common, uncommon, or bug-out crazy?

解决方案

Your coworker is wrong, the common way is and always has been to put code in .cpp files (or whatever extension you like) and declarations in headers.

There is occasionally some merit to putting code in the header, this can allow more clever inlining by the compiler. But at the same time, it can destroy your compile times since all code has to be processed every time it is included by the compiler.

Finally, it is often annoying to have circular object relationships (sometimes desired) when all the code is the headers.

Bottom line, you were right, he is wrong.

EDIT: I have been thinking about your question. There is one case where what he says is true. templates. Many newer "modern" libraries such as boost make heavy use of templates and often are "header only." However, this should only be done when dealing with templates as it is the only way to do it when dealing with them.

EDIT: Some people would like a little more clarification, here's some thoughts on the downsides to writing "header only" code:

If you search around, you will see quite a lot of people trying to find a way to reduce compile times when dealing with boost. For example: How to reduce compilation times with Boost Asio, which is seeing a 14s compile of a single 1K file with boost included. 14s may not seem to be "exploding", but it is certainly a lot longer than typical and can add up quite quickly. When dealing with a large project. Header only libraries do affect compile times in a quite measurable way. We just tolerate it because boost is so useful.

Additionally, there are many things which cannot be done in headers only (even boost has libraries you need to link to for certain parts such as threads, filesystem, etc). A Primary example is that you cannot have simple global objects in header only libs (unless you resort to the abomination that is a singleton) as you will run into multiple definition errors. NOTE: C++17's inline variables will make this particular example doable in the future.

As a final point, when using boost as an example of header only code, a huge detail often gets missed.

Boost is library, not user level code. so it doesn't change that often. In user code, if you put everything in headers, every little change will cause you to have to recompile the entire project. That's a monumental waste of time (and is not the case for libraries that don't change from compile to compile). When you split things between header/source and better yet, use forward declarations to reduce includes, you can save hours of recompiling when added up across a day.

这篇关于将C ++定义放在头文件中是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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