Is it possible to view all the contents of a namespace in c ++?

Issue

When you do the command:

using namespace std;

You get direct access to all elements of the std namespace. But suppose you want to use only the std::cout or std::endl so it would be better to use the directive:

using std::cout;
using std::endl;

So you would only get the objects you need to use not all.
My question is:
is there a way to view what is added when using the command:

using namespace std;

Something like: (I know this is highly wrong.)

#include <iostream>
using namespace std;

int main(){

cout << std;

return 0;
}

Solution

When you declare that you are using namespace std you tell the compiler that all the functions and objects within a specific namespace are accessible without having to prefix the namespace’s name.

You imported iostream which is actually just a header. But within this header are declared prototypes, and those are organized within namespaces (std in that case).

Depending of the C++ standard development libraries, the content of the file iostream may vary. However the implementation of the standard library is… standard.

Take a look at a source code example here: GCC – Libstdc++ iostream

You can see within the header the functions that are declared within the namespace std:

00043 namespace std _GLIBCXX_VISIBILITY(default)
00044 {
...
00061   extern istream cin;       /// Linked to standard input
00062   extern ostream cout;      /// Linked to standard output
00063   extern ostream cerr;      /// Linked to standard error (unbuffered)
00064   extern ostream clog;      /// Linked to standard error (buffered)
...
00067   extern wistream wcin;     /// Linked to standard input
00068   extern wostream wcout;    /// Linked to standard output
00069   extern wostream wcerr;    /// Linked to standard error (unbuffered)
00070   extern wostream wclog;    /// Linked to standard error (buffered)
...

Note that some IDEs (Visual Studio and cons) might provide you syntactic completion allowing you to view what is within a namespace or class scope.

Answered By – Fabien

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.