0

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?

4
  • iostream is a header (a file). std is a namespace, cout and cin are objects within the std namespace. Commented Mar 22, 2017 at 16:52
  • cout and cin are not functions, they are objects Commented Mar 22, 2017 at 16:52
  • @ryan iostream is also a type in the standard library - it's a typedef for basic_iostream<char> Commented Mar 22, 2017 at 16:53
  • This is a starting point: en.cppreference.com/w/cpp/header Commented Mar 22, 2017 at 17:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

It should be noted that the standard itself is inconsistent regarding the pluralisation. In §17.1, it talks about "the contents of the C++ standard library" and "the components in the library". But the section headings say, for example, "String library", not "String library component".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.