Showing 9 posts
Happy New Year! Let me welcome 2014 with a very simple post to wrap up the header files series as is customary so that you have all relevant links in a single place for future reference: Series introduction Multiple-inclusion protection Self-containment C++ ipp files Avoid C++ “using” directives Poor man’s replacement for modules Qualify your identifiers Poor compilation times in C++ As I mentioned a couple of posts ago, I am quite busy these days spending my limited free time on Kyua-related hacking, which means you should expect a less constant blogging pace.
Have you ever wondered why building C++ code is so awfully slow and, apparently, compilation times haven’t gotten any better over the course of many years? Wonder no more. This article on C++ Compilation Speed published on August 17, 2010 on Dr. Dobb’s delves into the problem. Mind you, a major cause of such slowdowns are header files and the fact that they are a horrible replacement for a modules system.
Apologies for the long pause in the header files series and in blogging in general. With the holidays in between and, especially, with my refreshed energy to do stuff in the FreeBSD and Kyua camps, I haven’t had any reasonable amount of time to write. And, sincerely, with my very limited free time I really cannot cover it all. So… these days, coding it is thus expect a reduced blogging pace!
I don’t like header files and, especially, I don’t like having to use them to implement modules. When you have used a language that sports real modules—pretty much anything other than C or sh—, header files feel primitive. Well, they actually are primitive. But what do I mean by “real modules”? Modules! In a language that supports modules, a module is a collection of related data types and code exposed via a well-defined set of symbols encapsulated in a single container (name).
Following up on the previous C++ post, here comes one more thing to consider when writing header files in this language. using and using namespace The C++ using directive and its more generic using namespace counterpart, allow the programmer to bring a given symbol or all the symbols in a namespace, respectively, into the calling scope. This feature exists to simplify typing and, to some extent, to make the code more readable.
Hoping you had a nice holiday break, it is now the time to resume our series on header files with a new topic covering the world of template definitions in C++. If you have ever used the Boost libraries, you may have noticed that aside from the regular hpp header files, they also provide a bunch of accompanying ipp files. ipp files, in the general case, are used to provide the implementation for a template class defined in a corresponding hpp file.
The rule The mere fact of including a given header file, without including any other beforehand, should not be enough of a reason for the build to break. This means that the header file should be self-contained, and for this to be the case, such header file has to pull in any dependencies that it explicitly requires (and no more). Interestingly, note that this does not mean that a header file must include everything it may ever need to be fully usable.
Let’s start the series with something simple: the basic structure of header files and why and how to protect against multiple inclusion. The rule The basic use of header files is to provide symbol declarations for functions and globals. Because multiple declarations of a given symbol in a single translation unit are a syntax error, you have to defensively structure your header files to not redefine anything in case they are included multiple times.
As I started typing a supposedly-simple post on header files, I realized that what I was explaining could easily be split into various posts and significantly extended with additional content, so here comes a new (short) series on these special little files in the need of careful attention. Quoting the Wikipedia: A header file is a file that allows programmers to separate certain elements of a program’s source code into reusable files.