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

Asked

Viewed 88 times

-1

When you make the directive:

using namespace std; //primeira forma

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

using std::cout;  //segunda forma
using std::endl;

So you would only get the objects that need to use not all. My question is: is there a way to visualize 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 read that it is preferable to use the second form of directive using mentioned than to use the general form. The book explained that usually the first form adds things that you don’t use. So I wanted to know if there is a way to visualize these 'things'. Anyway I already found the answer here https://stackoverflow.com/questions/45123958/is-it-possible-to-view-all-the-contents-of-a-namespace-in-c.

2 answers

2

You get direct access to all elements of the Std namespace

That is not true.

When you make the directive:

using namespace std;

you do not get direct access to all elements of the namespace std, just says you don’t need to put the namespace determined in front of each member of it, only there is no longer the need to qualify every identifier. He’s not an importer of anything so much that he needs to put the #include for everything you want to use.

This mechanism is just a name grouping, nothing more. It shouldn’t, but you can even create something and say it’s on std. Anyone can do this. So how do you list everything that exists? It doesn’t make sense.

Anything you want to use you should know well about it, so you don’t need to know about everything you have in a namespace, this is irrelevant.

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

  • That doesn’t answer the question. https://stackoverflow.com/questions/45123958/is-it-possible-to-view-all-the-contents-of-a-namespace-in-c/45123996#45123996 this does not answer.

  • 1

    I don’t get it, it basically says the same thing.

-1


In the sense of scope visibility throughout the programme,

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

Example.

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

In this case, you can access type Math::Point, data Math::Rigen and even field Math::Rigen. y, but you cannot access Math::Rigen.x. If you use using namespace Math ;, you will also write Point and Math::Point, you will also write the same Rigen and Math::Rigen, etc. Only you will not give in the same if there is something ambiguous, like something that has the same name outside the 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++, which makes it possible to know everything that is defined in a namespace. For example, just type "Std::" and as soon as you put the last character appears the list of classes, variables, functions, etc that are in "Std". You can select from the list (instead of typing) what you want to use, so it makes it easier to call the functions in case you don’t know the name of the function you want to use. Another way to know what’s in namespaces is to search the internet for libraries.

I don’t know any other way to do that. Even, the namespace I know exists only at compile time, so it is difficult to generate an output that involves the namespace itself instead of its content.

  • I believe you have not understood the question. What is desired is to put using namespace Math and, programmatically, discover that it has origen and Point inside the namespace

  • His question is not clear, it has multiple interpretations. It also makes it seem like he doesn’t quite know what namespace is. When he speaks in "using Std::Cout; using Std::Endl;", he speaks as if using it to give visibility.

  • 1

    I put a little more in my reply taking into account what you said.

  • Very much the editing part. I will read as calmly as possible.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.