Manohar
Manohar

Reputation: 3975

C++ standard header files

Accelerated C++: Practical Programming by Example book says the following..

... system header files need not be implemented as files. Even though the #include directive is used to access both our own header files and system headers, there is no requirement that they be implemented in the same way

What exactly does this mean? If not as a file how else can a system header file be implemented?

Upvotes: 4

Views: 97

Answers (3)

paxdiablo
paxdiablo

Reputation: 882816

The way in which headers are included into your "source file stream" is left mostly up to the implementation.

C++11 (but this has been the case for a long time, both in C++ and C) 16.2 Source file inclusion states:

A #include directive shall identify a header or source file that can be processed by the implementation.

A preprocessing directive of the form # include < h-char-sequence> new-line searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

(and then further description of the " and naked variants of #include).

So the header may be in a file.

It may also be injected by the compiler from hard-coded values.

Or read from a server located on one of the planets orbiting Betelgeuse (though, without FTL transmissions, such a compiler wouldn't last long in the marketplace).

The possibilities are many and varied, most of them bordering on lunacy but none of them actually forbidden by the standard itself.

Upvotes: 3

John Zwinck
John Zwinck

Reputation: 249652

Imagine you write your own compiler and C++ standard library. You could make it so that #include <vector> does not open any file, but instead simply loads some state into the compiler which makes it understand std::vector. You could then implement your vector class in some language other than C++, so long as your compiler understands enough to make it work "as if" you had written an actual C++ source file called vector.

Upvotes: 6

M.M
M.M

Reputation: 141633

The compiler could have hardcoded that when it sees:

#include <iostream>

then it makes available all definitions of things that are specified as being declared by this directive, etc.

Or it could store the definitions in a database, or some other encoded file, or the cloud, or whatever. The point is that the standard does not restrict the compiler in any way, so long as the end goal is achieved that the specified things get declared.

Upvotes: 5

Related Questions