Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's special about C++ header files with single-letter extensions?

The C++ Standard contains the following rule in section 16.2, Source File Inclusion. It makes single-character file extension special somehow.

The implementation shall provide unique mappings for sequences consisting of one or more nondigits or digits (2.11) followed by a period (.) and a single nondigit. The first character shall not be a digit. The implementation may ignore distinctions of alphabetical case.

What special treatment do these filenames get? What is a mapping in context of header file inclusion, and why does it matter if it is unique?

like image 503
Ben Voigt Avatar asked Mar 28 '14 00:03

Ben Voigt


1 Answers

It is saying, in the roundabout standardese way, that header files such as "abyssinia.h" shall be mapped to a unique file name in the file system, even if the underlying file system doesn't support 9.1 file names — think of old-style DOS with the 8.3 limitation, or the oldest versions of Unix that had a maximum of 14 characters for a file name. The system has to ensure that such names are mapped uniquely to different files.

It also says that the implementation may ignore case (which also means that it may not ignore case). If it does ignore case, then "ABYSSINIA.H" and "abyssinia.h" will map to the same file; if it does not ignore case, then they will be two separate files. Note that Windows and Mac OS X both have case-preserving but case-insensitive file systems, at least by default.

I'm not sure why the single non-digit restriction is present; it presumably means the the .hpp extension is not guaranteed to map to unique names.

This is all a constraint on the implementation; it mostly doesn't affect you as a programmer (unless you're a programmer writing the implementation — meaning the implementation of a C++ compiler), except that you should probably ensure that your header names are unique regardless of case, and for maximal portability, your header names should end .h or some other single-letter extension.

like image 114
Jonathan Leffler Avatar answered Oct 10 '22 13:10

Jonathan Leffler