Nikhil Raghavendra
Nikhil Raghavendra

Reputation: 1640

Hierarchy of namespaces, headers and objects

From what I understand, the namespace std contains all of C++ standard libraries, and one of the standard libraries is the iostream and it has the objects cout and cin.

std namespace 
       |
   iostream
       |
   cout, cin

Is the above structure correct? Or is it different?

Upvotes: 0

Views: 258

Answers (2)

JBL
JBL

Reputation: 12907

You are correct when you assume that everything that's in the standard library is contained in the std namespace, with a few exceptions, as per 17.6.1.1/§3 in the standard:

All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std. It is unspecified whether names declared in a specific namespace are declared directly in that namespace or in an inline namespace inside that namespace.

Then, when you say "the libraries", there's only one library here, the "standard library". iostream is a header of this library. But that doesn't mean everything from a header is in a specific namespace.

For example, cin and cout are directly in the std namespace, but are included with the <iostream> header.

Upvotes: 1

Christian Hackl
Christian Hackl

Reputation: 27538

From what I understand, the namespace std contains all of C++ standard libraries, and one of the standard libraries is the iostream and it has the functions cout and cin.

Not quite.

  • Although almost all of the standard library is in namespace std, we also have the C components outside of std when using the old header form (<stdio.h> instead of <cstdio>), and the assert macro, which is not scoped.
  • std::cin and std::cout are not functions but objects.
  • <iostream> is just the name of a header which contains parts of what the C++ ISO standard officially names "Input/output library" (std::iostream is a rarely used typedef for the std::basic_iostream<char> class).
  • Certain components of the library can be pulled in by different #includes. For example, std::initializer_list becomes available through <initializer_list> or through includes like <vector>.

Without going into too much detail, header files and scoping are two orthogonal concepts in C++. In other words, they exist in parallel to each other. There is no useful hierarchy among/between them.

Upvotes: 1

Related Questions