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

0

When you make the policy:

using namespace std; //primeira forma

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

using std::cout;  //segunda forma
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 everything that 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;
}

My question is in the following sense: I have read that it is preferable to use the second form of the mentioned using directive than to use the general form. The book explained that usually the first form adds things you do not use. So I wanted to know if there is a way to visualize these '' things ''. Anyway I already found the answer here link .

    
asked by anonymous 15.07.2017 / 19:17

2 answers

-1

In the sense of scope visibility throughout the program,

Yes, you can view all the contents of a namespace because you can not define private objects in the namespace scope (that is, if you do not create a class or structure scope or anything that makes it possible to privatize the data, will be public). And it is not necessary to use "using", as it only serves to make typing easier.

Example.

namespace Math {
    class Point {
        float x ;
    public:
        float y ;
    } ;
    static Point origen ;
}

In this case, you can access the Math :: Point type, the given Math :: origen, and even the Math :: origen.y field, but you can not access Math :: origin.x. If you use using namespace Math ; , you will also write Point and Math :: Point, it will also give the same origin and Math :: origin, etc. It just will not do the same if there is something ambiguous, like something that has the same name out of scope, then the use of the extended name with the scope specifies it, thus removing the ambiguity.

In the sense of human visualization of namespace resources,

Yes, there are editors, such as Visual Studio for Visual C ++, that let you know everything that is defined in a namespace. For example, just type "std ::" and as soon as you put the last character the list of classes, variables, functions, etc. that are in "std" appears. You can select from the list (instead of typing) what you want to use, so it makes it easier to call the functions if you do not know the name of the function you want to use. Another way of knowing what's in the namespaces is to search the internet for libraries.

I do not know any other ways to do this. Also, the namespace I know only exists at compile time, so it is difficult to generate an output that wraps the namespace itself instead of its content.

    
17.07.2017 / 23:17
1
  

You get direct access to all elements of the std namespace

This is not true.

When you make the policy:

using namespace std;

You get direct access to all elements of the namespace std and just say that you do not need to put the given namespace in front of each member of it, there is no more need to qualify all identifier. He is not an importer of anything, so much so that he needs to put #include to everything he intends to use.

This mechanism is just a grouper of names, nothing more than this. It should not, but you can even create something and say it's in std . Anyone can do this. So how to list everything that exists? It does not make sense.

Anything you want to use you should know well about it, so you do not need to know about everything you have in a namespace, that's irrelevant.

If you want to know what you have available, consult the documentation. There's nothing official, but a source close to it is the C ++ Reference .

    
15.07.2017 / 19:28