头文件中的C ++代码 [英] C++ code in header files

查看:144
本文介绍了头文件中的C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++的个人风格始终将类声明放在include文件中,并在.cpp文件中定义,这非常类似于 Loki对 C ++头文件,代码分隔的回答。不可否认,我喜欢这种风格的部分原因可能与编码Modula-2和Ada的所有年份有关,两者都有类似的规范文件和主体文件的方案。

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.

我有一个同事,在C ++中比我更熟悉,他坚持所有的C ++声明应该尽可能包括头文件中的定义。他不是说这是一个有效的替代风格,甚至是一个稍微更好的风格,而是这是每个人都在使用C ++的新的普遍接受的风格。

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?

推荐答案

你的同事是错误的,通常的方式是,总是把代码放在.cpp文件你喜欢)和头文件中的声明。

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.

最后,它常常令人讨厌

底线,你是对的,他错了。

Bottom line, you were right, he is wrong.

编辑:我一直在想你的问题。有一个的情况,他说的是真的。模板。许多较新的现代库(如boost)大量使用模板,通常是仅头。

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.

编辑:有些模板在处理模板时是唯一的方法。人们想要更多的澄清,这里有一些关于写头部代码的缺点的想法:

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

如果你搜索,你会看到很多人试图找到一种方法来减少编译时处理boost。例如:如何使用Boost Asio减少编译时间,这是看到一个14s编译单个1K文件与boost包括。 14s似乎不是爆炸,但它肯定比典型的更长,可以相当快地相加。当处理一个大项目。标题只有库确实以相当可衡量的方式影响编译时间。我们只是容忍它,因为boost是如此有用。

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.

此外,有很多事情,不能在标题只能做(甚至boost有你需要链接库部件如线程,文件系统等)。主要的例子是,你不能有简单的全局对象在标题只有lib(除非你诉诸可憎的单例),因为你会遇到多个定义错误。 注意: C ++ 17的内联变量将使此特定示例在将来可用。

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.

最后,

Boost是库,而不是用户级代码。所以它不会经常改变。在用户代码中,如果将所有内容放在标题中,每次小的更改都会导致您必须重新编译整个项目。这是一个巨大的时间浪费(而不是从编译到编译不改变的库的情况)。当你在header / source和更好的之间拆分时,使用向前声明来减少includes,你可以节省重新编译的时间,当一天添加时。

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天全站免登陆